PHP Scripting for IIS Administrators and Developers


An open-source alternative to ASP

\[Author's Note: PHP's Internet Server API (ISAPI) module is a new feature in PHP 4.0. Earlier versions of PHP were available only as Common Gateway Interface (CGI) executables. Although the ISAPI option is a significant leap forward for PHP, using the ISAPI version instead of the CGI version presents security and reliability concerns. (For information about these concerns, see PHP's README file.) Until the ISAPI version matures, I recommend that you use the CGI version of PHP in your production environments.\]

Have you been to a Web site recently and noticed files ending with .php or .phtml extensions? You might already know that these extensions denote PHP files. You might even know that PHP stands for PHP Hypertext Preprocessor. But what is PHP? In short, PHP is an open-source alternative to JavaServer Pages (JSP), CGI scripting languages, Allaire's ColdFusion, and Microsoft's popular Active Server Pages (ASP).

Microsoft succeeded with ASP in part because ASP is easy to learn and implement and is tightly integrated with a reliable, high-performance Web server—IIS. PHP shares these attributes with ASP, but it adds portability. PHP is available on a variety of Web servers and OSs. PHP is also a great way for Microsoft-focused Web professionals to get acquainted with open-source software without changing anything about their current architecture or environment. To get acquainted with PHP, let's

  • take a brief first look at PHP, including its benefits and a quick comparison with ASP
  • review the steps required to install and configure PHP on IIS
  • go over a practical example involving ODBC database publishing that you can put into production

First Look
PHP isn't a scripting engine but rather a scripting language like VBScript or JScript. PHP uses the Zend scripting language engine. PHP was developed as open-source software that used both Perl and UNIX scripting languages as a model. PHP is essentially HTML interspersed with code that script delimiters identify. PHP's well-documented API contains an extensive range of functions (or methods), including math functions, XML parsing, file and directory operations, and email handling. Other features, such as session support and include files, are also available.

Using library (API) functions that are available for all common commercial databases, PHP lets you interact directly with databases. For example, to establish a connection to a Microsoft SQL Server database, you can choose between a generic ODBC function or SQL Server-specific functions. One advantage of using direct database functions is that you don't have to create and maintain Data Source Names (DSNs) on your Web server. The most significant advantage is that direct database functions offer measurable performance advantages because the code that implements these functions has been tailored to interact with the database's native API. Keep in mind that if you use a database API to write your PHP code, you're effectively trading portability for performance. (You must specifically enable your database extensions in PHP's .ini file. By default, PHP supports ODBC without .ini file modification.)

PHP was developed as a Web scripting language. Although PHP will soon be extended for programming client-side programs, this capability will be too immature to use in the near future. This restriction might limit PHP's adoption by enterprises that need to reuse existing components or leverage the richer programming capabilities that COM or other commercial software development environments offer. However, if you're looking for a quick way to get reasonably powerful Web applications up and running, then consider PHP.

You can write ASP scripts in either JScript or VBScript. PHP closely resembles JScript in terms of syntax and functionality. For example, flow control in PHP and JScript are identical. JavaScript, Perl, Java, C, C++, and Microsoft's new C# all share a similar style and syntax. PHP doesn't yet meet the complete definition of an OOP language, although you can design code in an object-oriented manner. Table 1 provides a short list of the properties and features of both PHP and ASP.

PHP is a simple language to use and understand. Here are a few examples to illustrate this point. When you use HTML forms to send data to an ASP script for processing, you usually establish references to the form's data. You establish these references (or variables) differently depending on whether the form uses the HTTP POST or the HTTP GET method. To create these variables with ASP, use

Dim Price = Request.Form("Price")

if the form method is POST, and use

Dim Price = Request.Querystring("Price")

if the form method is GET.

With PHP, the HTML form data is immediately available for scripting purposes, without variable declaration. You simply use the variable $Price wherever you need to, no matter what the form method is. (PHP variables must start with a dollar sign—$—just like Perl. This stipulation makes it easy to identify variables within your scripts.) In fact, $Price could be a cookie value. Let's suppose you had previously set a cookie that contained the name-value pair Price=someValue. This data would be immediately available to your PHP script as $Price. Although PHP's default behavior is to build the GET, POST, and COOKIE variables automatically, this practice isn't recommended because of a potential security risk with poorly formed code. You can turn this feature off within the php.ini file. When you turn this behavior off, the Price GET variable, for example, would be accessible through $HTTP_GET_VARS\["Price"\]. (For information, see PHP's documentation.)

Here's another example. To display the date in the browser, you can call the date() function with arguments to control the date-display format. For instance,

echo date("m/d/Y")

displays 04/28/2001.

Benefits of PHP
As an administrator concerned with application-development strategy, you probably already use ASP to some extent. What would prompt you to incorporate PHP into your strategy or consider testing PHP? The two best reasons to consider PHP are portability and ease of use.

PHP can run on a variety of Web servers, including Apache and Netscape, which means that your code is portable, with some exceptions. For the best possible performance, PHP provides database-dependent libraries (APIs) for most commercial databases, including SQL Server, Oracle, and DB2. I recommend that you use these APIs only if you have a stable relational database management system (RDBMS) environment in which one database exists as a fixed standard for the entire term of the application's life cycle. Outside this situation, using database-dependent APIs to code an application is counterproductive and forces expensive code conversion in order to switch to a different database. PHP's ADODB or ODBC APIs are appropriate in most cases.

Probably the greatest benefit of PHP is its remarkable ease of use. ASP experience is helpful in learning PHP but not essential. PHP is a good way to introduce HTML coders to the realm of server-side scripting. I recommend that you have a solid foundation in HTML, not just visual tools such as Microsoft FrontPage or Macromedia Dreamweaver, before taking on PHP.

If you use good coding practices, PHP application performance is excellent. However, I recommend limiting PHP use to light Web applications (e.g., simple database applications, Web catalogs, email forms). A COM or Microsoft .NET solution is better for transaction-oriented or high-volume business applications.

Configuring IIS to Use PHP
PHP is a free download at and is available in two versions—CGI 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:

  1. 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.4p1—Patch Level 1.)
  2. Unzip the contents to a new folder. (For this example, use C:\php4.)
  3. 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.
  4. In the Application Configuration dialog box, which Figure 1, page 11, shows, click Add and select the Cache ISAPI applications check box.
  5. 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.
  6. 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.
  7. Copy C:\php4\php4ts.dll and the entire contents of D:\php\dlls to C:\winnt\system32.
  8. 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).

  1. 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.
  2. 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.
  3. 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
  4. <a href="getOdbcData.php?dsn=
    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.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.