Rolling Up Your Sleeves
Now that you've seen how to use MySQL and PHP to script Web sites, you have the tools to quickly and efficiently build a database-driven Web site of your own.
Because PHP is an open-source project, product support might not be what you're accustomed to with a commercial product. However, a number of online resources support developers, including FAQs and a developer-supported annotated online manual. If you run into trouble, you might want to start with the FAQs or the PHP support page.
Installing PHP version 3 on Windows NT
To install and run PHP, in addition to NT you need Microsoft IIS 4, currently distributed as part of the NT 4.0 Option Pack. You also need a database that's compatible with PHP; I use MySQL for the example listings.
Download the latest binary distribution of PHP version 3 for Win32 (see Resources, at the end of this sidebar). I used PHP 3.0.6 for the example listings in the article, and the distribution file was php-3.0.6-win32.zip. Any 3.x version will work fine for the example listings.
Unzip the zip file into a new directory, such as C:\PHP3.
In the new directory, rename the php3.ini-dist file to php3.ini and edit it with a text editor.
If this is your first time using PHP, you can leave most of the default settings in the .ini file unchanged. You do need to enable one of the database extensions so that PHP can communicate with a database. To follow the examples in the article, choose MySQL, which is readily available for Windows NT and many versions of UNIX. Enable MySQL by removing the semicolon the starts the line extension=php3_mysql.dll.
When you're finished editing, save a copy of php3.ini in your operating system directory, C:\WINNT. (Be careful if you have used Notepad, which always saves files with the .txt extension.)
Next, you need to edit the NT registry. (Warning: If you damage the registry, you can cause major system problems. Make sure you have backed up before you edit the registry.)
- From Windows, select Start>Run and type regedit to start the registry editor.
- Go to the entry for HKEY_LOCAL_MACHINE:System:CurrentControlSet:Services:W3Svc:Parameters:ScriptMap.
- Choose Edit >New >String Value.
- In the highlighted area under Name, type .php3 and then press Return.
- Double-click on the same highlighted area.
- In the Edit String box that pops up, enter the value C:\PHP3\PHP3.EXE and then click OK.
Next you need to start up the Microsoft Management Console.
- From Windows, choose Start>Windows NT 4.0 Option Pack>Microsoft Internet Information Server>Internet Service Manager.
- Right-click the name of the Web server and select Properties from the menu that opens.
- Click the Home Directories tab and select Script under Permissions.
- Click Configuration. In the Application Configuration pop-up window that appears, select Add
- Enter C:\PHP3\PHP.EXE %s %s for Executable, .php for Extension in the popup window.
- Make sure that Script Engine is enabled.
- Click OK to close the Add/Edit Application Extension Mapping window.
- Click OK in the Application Configuration window.
- Restart the IIS server.
Now that you've installed PHP, it's a good idea to test that it's working.
- Enter a test script, such as the one in Win NT Installation Listing 1.
- Save the script where your browser can access it. For example, you can put it in C:\INETPUB\WWWROOT\PHP\TEST.PHP3.
- Open a browser.
- Access the test script from your browser. In the example, you can access the script with the URL http://localhost/php/test.php3.
Installing PHP version 3 on UNIX
To begin installation, you need to first assemble the software you need. including Apache 1.3.x, if you don't already have it installed (see Resources at the end of this sidebar). For this article, the distribution file was apache_1.3.6.tar.gz. Also download the source code for PHP version 3.. For this article, I used the PHP distribution file php-3.0.9.tar.gz, but any 3.x version will work. You also need a database that's compatible with PHP; I use MySQL for the example listings.
Put the files apache_1.3.6.tar.gz and php-3.0.9.tar.gz in the same directory. The /tmp directory is a good place, if you have sufficient space on that partition. (I needed 38MB to build Apache and PHP under Linux.)
Next you configure, build, and install in /opt/mysql a PHP-enabled Apache Web server that can access a MySQL database by entering the commands in UNIX Installation Listing 1.
PHP version 4 debuts in beta
./configure --with-mysql=/opt/mysql --with-apache=../apache_1.3.6 --enable-track-vars --with-config-file-path=/opt/apache/conf
./configure --prefix=/opt/apache --activate-module=src/modules/php3/libphp3.a
Lines 4 and 10 call for Apache to be installed in /opt/apache rather than the default location of /usr/local/apache. Line 6 instructs PHP to build with MySQL, which assumes that you have installed MySQL into /opt/mysql rather than the default /usr/local/mysql. You can build with support for other databases drivers, but the examples in this article call for MySQL. Type configure --help to see the command-line options needed to add support for the other database software.
Once Apache has been installed, you edit the httpd.conf file. If you installed Apache in /opt/apache as shown in the listing above, the configuration file will be /opt/apache/conf/httpd.conf.
In the configuration file, uncomment the line AddType application/x-httpd-php3 .php3.
Note: With PHP version 4 now available in beta release, the PHP authors are encouraging developers to switch from using the file extension .php3 to .php instead. To use the .php extension, add an additional line in the configuration file that says AddType application/x-httpd-php3 .php. (Note that the files distributed in the demo use the php3 extension.)
Now it's time to start the server. If you installed according to my instructions, use the command /opt/apache/bin/apachectl start.
See your operating system's documentation to find out how to start Apache automatically at boot time, as the method varies widely among the different versions of UNIX.
The first beta release of PHP version 4 occurred in July. PHP4 uses the Zend scripting language engine, a complete rewrite of the scripting engine for PHP. Although Zend currently is used only with PHP4, Zend eventually will be used as the scripting engine for products other than PHP.
The most ambitious goal of this major update is to improve performance. Preliminary benchmarks performed by the PHP/Zend developers show PHP 4.0 outperforms PHP 3 by a factor of five or more on most applications. The same benchmarks also show that PHP 4.0 compares favorably to ASP's performance.
PHP4 includes a number of other improvements. The new features of PHP4 include:
- A full-featured debugger, with breakpoints and step-through execution.
- Accessing COM objects, which are abundant for Windows.
- Output buffering to allow you to abort the page in the middle of the script and send a redirect header instead.
- Automatic resource deallocation, which means that there's no need to remember to free resources in PHP4 because they're deallocated soon as they are no longer referenced.
- A new foreach loop.
- Boolean terms true and false are predefined.
- Reference support, much like C-style pointers, so that you can use $foo = &$a to make $foo and $a two names to the same variable.
In August, developer Zeev Suraski indicated that PHP 4.0 would most likely be released in November or December. However, the schedule is subject to change based on feedback from the beta releases. As an open-source project, it will be released when the developers decide the product is ready.