Programming PHPProgramming PHPSearch this book

Chapter 15. PHP on Windows

Contents:

Installing and Configuring PHP on Windows
Writing Portable Code for Windows and Unix
Interfacing with COM
Interacting with ODBC Data Sources

There are many reasons to use PHP on a Windows system, but the most common is that you want to develop web applications on your Windows desktop machine without the hassle of telnetting into the central Unix server. This is very easy to do, as PHP is extremely cross-platform friendly, and installation and configuration are becoming easier all the time.

What can be confusing at first is the number of various configurations and choices available. There are many variants of the Windows operating system, and many web servers are available for those operating systems. PHP itself can run as either a dynamic link library (DLL) or a CGI script. It's easy to get confused or to misconfigure your system. This chapter explains how to install, configure, and make the best use of PHP on Windows systems. We also show how to take advantage of the features unique to the Windows platform—connecting to databases with ODBC and controlling Microsoft Office applications through COM.

15.1. Installing and Configuring PHP on Windows

This section shows you how to install PHP on Windows. We cover both manually configuring your web server to use PHP, and the use of the PHP installer, which will do the configuration for you.

15.1.1. Going Straight to the Source

The most recent version of PHP can always be found at http://www.php.net/downloads.php. While you could download the source and compile it yourself, chances are you don't have a compiler. Fortunately, the PHP downloads page has a binary distribution for Windows.

Download the latest Windows PHP distribution and extract it into a local directory. You'll need a program such as WinZip (http://www.winzip.com) to extract the ZIP file. At the root level of the distribution is php.exe, which you can run from a command prompt to test and experiment with PHP. If you have PHP code in a file (e.g., test.php), you can run that code with:

C:\> php -q test.php

15.1.2. Configuring PHP with a Web Server

Once you have PHP on your local computer, the next thing to do is to configure it into a web server.

The choices here are many. PHP can either be run as a standalone CGI script or linked directly into the server via the server's native Server API (SAPI). There's SAPI support for IIS, Apache, Netscape iPlanet, and AOLserver. PHP can even be configured to run as a Java servlet engine.

Because of the rapid change in the development of PHP, it is always best to check with mail lists and online resources to determine the best configuration for your specific application. In general, the CGI version is more reliable, but it is slower than SAPI implementations because it has to be loaded with each request. SAPI implementations load once and create a new thread for each request. Although this is more efficient, the tight coupling with the server can bring the entire server down if there are memory leaks or other bugs with an extension. SAPI support on Windows is considered to be unstable as of the writing of this book, and hence is not recommended for production environments.

For our discussion, we will look at and compare installation on Microsoft Personal Web Server (PWS) and Apache for Windows, both on Windows 98—two installations that help to contrast the differences in implementation while providing useful local development environments.

15.1.2.1. Configuration common to all Microsoft installations

Regardless of the server you use, there are a few steps common to all installations in a Microsoft environment:

  1. Decide where to extract the distribution. A common location is c:\php.

  2. Copy the php.ini.dist file to c:\windows\php.ini, or specify the location in the PHPRC environment variable. Edit the file to set configuration options.

  3. Ensure that the system can find php4ts.dll and msvcrt.dll. The default installation has them in the same directory as php.exe, which works. If you want all your system DLLs together, copy the files to C:\WINDOWS\SYSTEM. Alternatively, add the directory containing the PHP DLLs to the PATH environment variable.

DLL search order varies slightly between versions of Windows. In most cases, it is as follows:

  1. The directory from which the application loaded

  2. The current directory

  3. Windows 95/98/Me: the Windows system directory; Windows NT/2000 or later: the 32-bit Windows system directory (SYSTEM32)

  4. Windows NT/2000 or later: the 16-bit Windows system directory (SYSTEM)

  5. The Windows directory (WINDOWS)

  6. The directories listed in the PATH environment variable

15.1.3. Adding Extensions to the Base Distribution

PHP on Windows has out-of-the-box support for ODBC and MySQL. Most other extensions must be manually configured (i.e., you must tell PHP where to find the DLL files).

First tell PHP which directory contains the extensions by adding this to your php.ini file:

extension_dir = C:\php\extensions; path to directory containing php_xxx.dll

Then explicitly load the module with a line like this in the php.ini file:

extension=php_gd.dll; Add support for Tom Boutell's gd graphics library

You can determine what extensions are available for your particular version by looking in the extensions directory of your distribution.

Once you have made these changes, restart your server and check the output of phpinfo( ) to confirm that the extension has been loaded.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.