Archive

Archive for the ‘Code’ Category

create a mysql user that can connect from anywhere to anything

February 3rd, 2004 Tony 1 comment

Obviously this is a huge security mistake to create this on a production server but if you just want to be able to get to all databases on a remote server in your intranet its a necessity:

On the server:

Login as root: mysql -u root -p

Create user: GRANT ALL PRIVILEGES ON *.* TO myuser@’%’ IDENTIFIED BY ’some_password’ WITH GRANT OPTION;

Now you can login from another machine

mysql -h 192.168.0.100 -u myuser -p

Categories: Code Tags:

cURL with PHP and Apache on Windows

October 22nd, 2003 Tony 122 comments

Setting up cURL my linux server it was no problem at all, but I had a heck of a time getting cURL to work properly on my Windows test box with PHP and Apache. There are a lot of tricks scattered around on the web so here is my list of notes:

1. Only install PHP with the zip’d binaries. Don’t use the installer. I recommend deleting your current PHP installation and reinstalling with the binaries. Downloading the latest PHP has the added benefit of ensuring its compatible with the version of cURL you’ll download later. (I installed to D:\apps\php and will use that path for the rest of this example)

2. Edit your php.ini file:

   - set extensions_dir to d:\apps\php\extensions

   - set register_globals to On

   - set sessions.save_path to d:\apps\php\temp (you need to create that directory first)

3. Copy php4ts.dll (located in d:\apps\php\) to your Apache bin folder

4. Copy libeay32.dll and ssleay32.dll (located in d:\apps\php\dlls\) to c:\windows\system32

5. Download cURL for Windows at: http://curl.haxx.se/download.html. I chose the Win32 – Generic by J?Hartroth. I recommend getting the SSL version in case you ever need SSL. I unzipped cURL to d:\apps\curl and will use that path for the rest of this example

6. [SSL INSTALL ONLY] Download OpenSSL for Windows from http://curl.haxx.se/download.html. (Its near the bottom of the page). Extract libssl32.dll to d:\apps\curl

7. [Windows XP Install Only] Check to see if you have the following file: c:\windows\system32\msvcr70.dll. If not, search for it in Google and download it to system32. You may get error messages without it.

8. Uncomment the curl line in your php.ini file to enable curl: extension=php_curl.dll

9. Finally edit your Apache httpd.conf file to enable php:

   - Uncomment: LoadModule php4_module d:/apps/php/sapi/php4apache2.dll

   - Add Line: AddType application/x-httpd-php .php

Test with the following PHP code:

   $url = “http://www.thinkgeek.com”;

   $ch = curl_init();

   curl_setopt($ch, CURLOPT_URL,$url);

   curl_setopt($ch, CURLOPT_VERBOSE, 1);

   curl_setopt($ch, CURLOPT_POST, 0);

   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

   $returned = curl_exec($ch);

   curl_close ($ch);

   echo $returned;

SSL NOTE: I kept getting no response when I tried using curl with SSL urls. I found that adding the following solved the problem:

   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

I have read that the proper solution is to use the ca-bundle.crt file for curl to be able to verify certificates but I haven’t tried this yet:

   curl_setopt($ch, CURLOPT_CAINFO, ‘drive:\pathto\ca-bundle.crt’);

Categories: Code Tags:

java URLConnection and timeout

June 12th, 2003 Tony 1 comment

Throw this into your bag of goodies:

If you find yourself needing to make connections to other web servers from Java you’ll quickly run into problems. I found this nice package that provides a lot of functionality that is lacking in java URLConnection. The nicest things are the ability to read all response codes and set a timeout for the connection.

http://www.innovation.ch/java/HTTPClient/

http://www.innovation.ch/java/HTTPClient/urlcon_vs_httpclient.html

Categories: Code Tags:

my feelings about torque – an update

January 9th, 2003 Tony No comments

my post in october about how great Torque is might be misleading. after finishing raleighlist which uses Torque for persistence i can still say its absolutely pukka but has its problems.

the biggest problem i encountered was a bug in which it fails to retrieve reliable connections from its database pool. it seems like it should be validating the connection but i found it to fail quite often. i just received an email from another user on the turbine mailing list that is experiencing the same issue on tomcat and oracle. my workaround was to hack the torque source code so that it uses resin’s connection pooling. problem solved.

then next problem i encountered was performance. when performing certain selects on tables with a large number of records, the performance degraded quickly. i’m sure there are tweaks i could implement and perhaps i just need to explore proper indexing techniques but instead i just wrote jdbc code in poor performing areas and it now performs extremely well even with 50k records.

Categories: Code Tags:

Torque

October 3rd, 2002 tony 2 comments

Well I’m working on a new project and I thought I would test out the Jakarta project Torque for persistence layer. The setup was pretty easy although it took me a little longer to figure out how to configure it with Resin and db pooling.

You use XML to define your objects like so:

< table name="author" description="Author Table">

< column

name=”author_id”

required=”true”

primaryKey=”true”

type=”INTEGER”

description=”Author Id”/>

< column

name=”first_name”

required=”true”

type=”VARCHAR”

size=”128″

description=”First Name”/>

< column

name=”last_name”

required=”true”

type=”VARCHAR”

size=”128″

description=”Last Name”/>

< /table>

Once you setup your schema the coding is easy peasy:

INSERT

Book book = new Book();

book.setTitle(title);

book.setISBN(isbn);

book.save();

SELECT

Criteria crit = new Criteria();

crit.add(BookPeer.title, “Thinking in Java”);

List list = BookPeer.doSelect(crit);

There are ant tasks defined for creating the database, generating the tables, pre-populating tables, and generating your source code. It has transactional support, object and method caching, and a variety of methods of db pooling built in. It also can perform cascading deletes.

Another cool thing is that you can create criteria to use SQL joins to get an object and its foreign key objects thereby elimating multiple selects for an object.

Pretty cool. I’m anxious to see how it performs under a load.

Categories: Code Tags: