[main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql]

HVCC Home
Blackboard HVCC
Blackboard Manual
Faculty Association

php main
1. what is php
a. php history
b. uses of php
c. php hello world
* d. installing php
2. http basics
3. php basics
4. php expressions
5. php client side
6. php flow control
7. php manual


print version

Note that all external links will open up in a separate window.

This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser.

Directions for surfing with audio.

Installation

These tutorials are about PHP and its use for server-side Web programming.

Choose Your Tools

If you aren't comfortable experimenting with your computer, or with failing to successfully install something on the first try, you probably want to entirely ignore this topic.

I realize that PHP is cross-platform. However, all I have is a Windows box. Thus my directions are specific to that. As soon as the school sees fit to equip me with a whole variety of computers for application testing, I will be more than happy to expand this section.

Although you could run Microsoft Personal Web Server (PWS) or the Internet Information Server (IIS), I am partial to Apache. Thus we will discuss how to install Apache on your computer and how to install PHP. Since Apache is now built into Apple's OS/X and most flavors of Linux, it also gives us a nice standard base to work from.

It is also much easier to get under the hood of Apache than it is with any other HTTP server.

On the other hand PHP does have an auto-installer for PWS and IIS, so if you have one of these running, you don't have to do any manual editing of your configuration files or copy and renaming of files in order to get PHP up and running.

In order to proceed you will need the following:

  • a computer, preferably your own
  • a reasonably speedy Internet connection
  • a hard drive that is not yet totally maxed out
  • (for Apache) a zip tool
  • (for Apache) the Microsoft Installer (should be built into Windows on all newer computers) -- unless you want to do things the hard way
  • patience

Installing Apache

To install Apache, you first need to go to [Apache.org] and download it. The downloads can be found under the HTTP Server link on the left menu.

There are two currently available up-to-date versions of Apache.

  • Apache 2.0
  • Apache 1.3

Apache 2.0 has more toys and even a GUI interface for basic aspects of its operations, such as turning it on and off and determining what Internet services you have running. It is, however, not guaranteed to work with PHP. I am currently running it with no problem, but it is worth mentioning the caveats. Apache 1.3 is an older, more stable version, and PHP 4 is specifically designed to work with it. The directions for installing PHP that come with the download are for Apache 1.3. However it doesn't seem to be a problem to get PHP set up for Apache 2.0. My directions largely assume you are using Apache 2.0, but feel free to take your pick.

To download Apache 2 you want to find apache_2.0.43-win32-x86-no_ssl.msi on the download page, unless it has been superceded by a newer version. The current version of Apache 1.3 is apache_1.3.27-win32-x86-no_src.msi. Download one of these files to a location where you can find it. The Desktop is always a useful temporary storage site.

To install Apache from the MSIE file, you just have to double click on the file and it should invoke the Microsoft Installer.

You should have a recent version of Microsoft Installer in order to install the software. Fortunately, if you don't have a recent installer, newer versions of Windows should automatically take you the Microsoft download site when you try to invoke the installer. If it does not, then look on the Apache download page for the link to important notes for Windows users.

At the appropriate prompt screen in the install process, you will want to set the network domain and server name to localhost. You should also set the e-mail address to one that you use, or to me@localhost (or anything else at localhost, it's not a real e-mail address). You can leave everything else with the default settings.

Apache 2 comes with a control console which should show up as a little icon in your system tray (that space just to the left of the clock on the task bar). Double-clicking it will open the Apache console, where you can start and stop Apache as well as connect to remote computers and determine what local services you have running.

Image of the Apache 2 control console

If you installed Apache correctly, Apache and the Apache console should already be up and running. Now we just have to tweak it for your computer.

Setting-up Apache

The first thing you want to do is stop Apache. If you did not install Apache 2 and/or do not have the Apache console, you can stop Apache from the Start menu. It should be found under something like Start >> All Programs >> Apache HTTP Server >> Control Apache >> Stop. You will also find the commands to start and restart the server there.

Then you want to edit the configuration file. Your Apache sub-directory in the Start menu should include an edit function on one of its sub-menus. Otherwise, the file you want is named httpd.conf and it can be found under
C:\Program Files\Apache Group\Apache2\conf
If you are using Apache 1.3, then the directory name is Apache instead of Apache2. Open httpd.conf in your favorite text editor.

Note there there is also a file called httpd.default.conf. Do not change that one. You use it to overwrite the configuration file back to its default settings if you mess things up.

The httpd.conf file is the file that contains all the basic configuration information for running Apache. Most of it is comments, which is anything preceded by a hash mark ( # ). We need to make a few changes to this file to make sure Apache knows where to find our files.

First you want to find the following location in the code:

#
# DocumentRoot: The directory out of which ...
# documents. By default, all requests are  ...
# symbolic links and aliases may be used   ...
#
DocumentRoot "C:/Program Files/Apache Group/Apache2/htdocs"

We want to change our document root to the name of the directory we are storing our Web pages in. For instance, I do all of my development in a directory called webdev, so my document root is:

DocumentRoot "C:/webdev"

Notice that even though it is a Windows path name, Apache is expecting forward slashes.

You also need to find the following section and change it to point to the same directory as your DocumentRoot.

#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "C:/Program Files/Apache Group/Apache2/htdocs">

If you save this file and then start Apache up again, assuming you didn't make any mistakes, you should be able to open up a Web browser, type localhost into the address bar, and it should display the contents of the directory you specified as the DocumentRoot above. If you haven't changed the document root directory, then it will display an Apache welcome page.

If you have gotten this far, then you are ready to add in the PHP.

Installing PHP

Installing PHP manually is not that hard, but does involve some trial and error. We need to download PHP, install it, configure it, and then edit the Apache httpd.conf file to tell Apache where PHP is and what to do with it.

The place to start for PHP is [PHP.net]. It is the official PHP Web site. To go to the download page, just click downloads on the menu bar on top. It is a discrete menu bar with small blue text on a blue background.

If you are running Apache, then you want to download
PHP 4.3.0 zip package [5,811Kb] - 27 December 2002.
If you are running PWS or IIS, then you can download
PHP 4.3.0 installer [1,028Kb] - 27 December 2002.
If there is a more recent stable version available for either option, then you can grab that instead.

The installer is a Microsoft Installer file and is self-configuring. You double click and pretty much let it run. We won't talk about it here. We will talk about the Zip package, which you do have to manually configure.

If you are using the Zip file, download it and then unzip it into some directory. C:\ is probably a good one, since the installation directions that come with it assume that this is where it is. The contents all reside in a single directory called php-4.3.0-Win32. Make life easy on yourself and rename the directory to just php. Then you are ready to continue.

First, familarize yourself with the files that are there and read the included installation directions in the install.txt file. Read it a few times until it begins to make a weird sort of sense. Then try to follow the directions. The rest of this document is really just advice on following the official installation directions.

This is where trial and error begins to come into play.

If you are using Apache 2, then do not copy the php.ini-dist file to the system directory. Instead, copy it to the Apache 2 directory (C:\Program Files\Apache Group\Apache2). (Well, first make a copy of it called php.ini, and then move that file to the new directory. The php.ini-dist is your original configuration file and you want to save that in case you need to go back to the default settings.) Alternately, you can make a copy of the php.ini-recommended file instead. It has more stuff in it and creates a more secure server environment.

For Apache 1.3, the newly created php.ini file should go where the installation directions tell you to put it.

Move all the DLL files where they tell you to, paying attention to the differences between Apache and Apache2 directions. If you are uncertain of the system directory to which to move the DLL files from the dlls directory you will either have to experiment or move them to all possible locations. If you use the second approach, when you get comfortable with the software, you can experiment with deleting them until you figure out which deletions cause the software to stop working.

Whether they go in windows\system or windows\system32 or c:\winnt\system32 is entirely dependent on your configuration, which may vary much more than is apparent by the installation directions. I am runnning Windows XP and have mine in my windows\system32 directory.

Next you want to edit your php.ini file.

Setting up PHP

Open the php.ini file in the text editor of your choice. You may have multiple copies of the file floating around by now, so make sure you are editing the right one.

First, search for the line
short_open_tag = On
and change it to
short_open_tag = Off
It will make learning to code a little easier by making the edits a little stricter. It will also make the PHP engine XML-compatible. If you plan to work with XML, you must have the short_open_tag turned off because otherwise it will conflict with XML processing directives.

be forewarned that the school still has short_open_tag set to On in spite of repeated protestations on my part. Fortunately, the only place that will mess you up on my PHP class is when using an XML prologue in an XHMTL document.

Next, look for the section beginning with:

;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;

Notice the semi-colon at the front of each line. The PHP configuration line uses semi-colons to comment out a line.

Just below this heading you want to set the doc_root variable to the same path as you set for the Apache DocumentRoot above. Unlike Apache, you can use backslashes in the path name.

For me this is:

; The root of the PHP pages.
doc_root = c:\webdev

You also want to set your extension_dir to wherever you will put your PHP extention modules. All the extentions are stored in the extensions subdirectory of the PHP install, so if you accepted the default placement above, you can point directly to them with:

; Directory in which the loadable extensions (modules) reside.
extension_dir = c:\php\extensions\

I prefer to download all active extensions to my main PHP directory, so for me it is:

; Directory in which the loadable extensions (modules) reside.
extension_dir = c:\php\

If you put PHP somewhere else you will need to use a different path.

That is all you need to change for now, so save the file.

Telling Apache About PHP

We now need to tell Apache where PHP is and what to do with it. So, open the httpd.conf file again.

Now find where it says:

#
# DirectoryIndex: Name of the file or files   ...
# directory index.  Separate multiple entries ...
#
<IfModule mod_dir.c>
    DirectoryIndex index.html
</IfModule>

This is where you tell Apache what files to look for as default files to be loaded when a file is not specified in the requested address. index.html is already listed. In a space separated list, we are going to add a few more options. For us, the last one is the most important

<IfModule mod_dir.c>
DirectoryIndex index.html index.htm index.php
</IfModule>

Now find the #LoadModule section of the configuration file and add the following lines of code.

For Apache 1.3.

LoadModule php4_module c:/php/sapi/php4apache.dll
AddModule mod_php4.c
AddType application/x-httpd-php .php

For Apache 2, the module is automatically added if it is loaded, so the second line can be removed. In fact, to get it to work you have to remove it. Also, you want to reference the php4apache2.dll, instead of the php4apache.dll.

LoadModule php4_module c:/php/sapi/php4apache2.dll
AddType application/x-httpd-php .php

The first (and second) line tell Apache to load and start up the core PHP module for use by the server. The last line says to associate files ending in a .php suffix with the PHP modules.

Save the file. If you missed it in the official installation directions, make sure you have copied the files name php4ts to the sapi directory. They need to be in the same directory as the php4apache(2).dll.

Now try to restart Apache.

If you get no errors, you should be all set. Put the PHP hello world exercise in your document root directory and see if you can access it in a Web browser using localhost.

If it says it is unable to start, then try moving the php.ini file around to the Windows directory and to the various system directories until Apache successfully starts.

Yes, this is the big trial and error bit, getting the all the files placed in the right locations. There is no easy way around this with Apache. If you don't want to take on the trial and error method, I strongly advise you to try running PHP through PWS. That was you can use the PWS auto-installer, if you don't already have it installed, and the PHP auto-installer to configure it for PWS.

If you mess up using the Apache approach, you can always just delete it all and start again. It has the benefit of the damage being isolated and not likely to mess up your computer. You can also try the directions for the PHP executable, just be aware that it is a major security risk if you are online with Apache running to use the PHP executable. It is really for development only.

If you want some extra reading on the topic, you can also look at the PHP manual.
In HTML format: [PHP Manual]
In Windows help file format, which is a 3Mb file, so you are probably better of saving the file and opening it locally if you want to use this approach: [PHP Help Pages]

[top]