Configuring IIS to Use PHP
PHP is a free download at http://php.net and is available in two versionsCGI and ISAPI. As an ISAPI module implemented as a DLL, PHP offers better performance than the CGI version implemented as an executable (i.e., php.exe). If your server has a significant load, opt for the ISAPI version. To install the ISAPI version for IIS, follow these steps:
- Download the latest version of PHP. Make sure you're downloading the correct Win32 binary version. (At the time of writing, the latest version is 4.0.4p1Patch Level 1.)
- Unzip the contents to a new folder. (For this example, use C:\php4.)
- Open the Microsoft Management Console (MMC) Internet Information Services snap-in (in IIS 5.0) or the MMC Internet Information Server snap-in (in IIS 4.0), right-click the site you want to PHP-enable, and select Properties. Click the Home Directory tab, then click Configuration.
- In the Application Configuration dialog box, which Figure 1, page 11, shows, click Add and select the Cache ISAPI applications check box.
- Add a new application mapping for the .php extension that php4isapi.dll (e.g., C:\php4\sapi\php4isapi.dll) will process. Select the Script engine check box, as Figure 2 shows.
- Copy C:\php4\php.ini to the system root (i.e., C:\winnt). You don't have to edit php.ini to get PHP running. You should go through the extensive list of options within this file later and edit it to suit your requirements.
- Copy C:\php4\php4ts.dll and the entire contents of D:\php\dlls to C:\winnt\system32.
- Restart IIS.
Within IIS, I recommend enabling ISAPI caching for maximum performance. You also need script permissions to access PHP pages.
To test your new installation, create a new file called phpinfo.php in a text or HTML editor. Enter the line
<? phpinfo() ?>
Save the file to your IIS Web server document folder (e.g., D:\inetpub\wwwroot). Open a browser to http://localhost/phpinfo.php, and you'll see a PHP page like the one Figure 3 shows.
Code Example
Here's an everyday example of PHP that you can put to use immediately. Let's say that you're responsible for maintaining domain names for 50 sites that your servers host. You keep a Microsoft Access database with all the pertinent information you need to manage your environment. One table in your database contains the domain name registration information. Management wants to know what domain names are registered and when they expire. The accounting department also needs access to this information. Rather than share the Access database with multiple people in multiple departments, you can post the information on the company intranet Web server.
Your goal is to keep the information dynamic, and you don't want to waste time. Using the procedure and code that I present here, you can have a simple solution ready in no time (or 20 minutes).
- Define an ODBC DSN to the database that contains the data you need to display. For this example, I have an Access database with a DSN labeled Php1. Within this database, a Domains table exists that contains a field named DomainName. I recommend that you make this DSN read-only to prevent unauthorized updates to the data.
- Copy the code in Listing 1 and Listing 2 to your PHP-enabled site. (These files are coded so that you can access any database with a DSN.) Listing 1 handles user or database errors.
- Use the code in Listing 2 to create and test a hyperlink to the data that you want to display. The hyperlink for this example is
<a href="getOdbcData.php?dsn=
Php1&table=Domains&order=
DomainName">
Domain Name Data</a>
You can easily improve the functionality and design of this simple application. Cascading Style Sheets (CSS) can help you improve the presentation considerably. With some practice and knowledge of PHP's API, you'll be able to create reliable applications for your Web server in record time.
Note: With a slightly modified installation procedure, PHP will also run on Personal Web Server (PWS) on Windows Me and Windows 9x.
Note: PHP is case sensitive. For example, $price and $Price are different variables.