Developer Forums | About Us | Site Map


Useful Lists

Web Host
site hosted by netplex

Online Manuals

Script Web databases quickly with PHP
By Craig Knudsen - 1999-09-01 Page:  1 2 3 4 5 6 7 8

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 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.)

  1. From Windows, select Start>Run and type regedit to start the registry editor.
  2. Go to the entry for HKEY_LOCAL_MACHINE:System:CurrentControlSet:Services:W3Svc:Parameters:ScriptMap.
  3. Choose Edit >New >String Value.
  4. In the highlighted area under Name, type .php3 and then press Return.
  5. Double-click on the same highlighted area.
  6. 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.

  1. From Windows, choose Start>Windows NT 4.0 Option Pack>Microsoft Internet Information Server>Internet Service Manager.
  2. Right-click the name of the Web server and select Properties from the menu that opens.
  3. Click the Home Directories tab and select Script under Permissions.
  4. Click Configuration. In the Application Configuration pop-up window that appears, select Add
  5. Enter C:\PHP3\PHP.EXE %s %s for Executable, .php for Extension in the popup window.
  6. Make sure that Script Engine is enabled.
  7. Click OK to close the Add/Edit Application Extension Mapping window.
  8. Click OK in the Application Configuration window.
  9. Restart the IIS server.

Now that you've installed PHP, it's a good idea to test that it's working.

  1. Enter a test script, such as the one in Win NT Installation Listing 1.
  2. Save the script where your browser can access it. For example, you can put it in C:\INETPUB\WWWROOT\PHP\TEST.PHP3.
  3. Open a browser.
  4. Access the test script from your browser. In the example, you can access the script with the URL http://localhost/php/test.php3.
Listing 9. A simple script to confirm successful installation on Win NT

Listing 9
 <?php phpinfo(); ?>

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

Listing 10. Commands for installation and configuration on UNIX

  1. gunzip -c
  2. gunzip -c
  3. cd apache_1.3.6
  4. ./configure --prefix=/opt/apache
  5. cd ../php-3.0.9
  6. ./configure --with-mysql=/opt/mysql --with-apache=../apache_1.3.6 --enable-track-vars --with-config-file-path=/opt/apache/conf
  7. make
  8. make install
  9. cd ../apache_1.3.6
  10. ./configure --prefix=/opt/apache --activate-module=src/modules/php3/libphp3.a
  11. make
  12. make install

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.

View Script Web databases quickly with PHP Discussion

Page:  1 2 3 4 5 6 7 8 Next Page: Resources

First published by IBM developerWorks

Copyright 2004-2019 All rights reserved.
Article copyright and all rights retained by the author.