Using PHP With Apache

To create a more three dimensional web site you will need to add various features to make it interactive. This just means that the web page visitor gets to do something besides just read your content. This will usually entail some kind of web form and that wiil require something to read and act on what's entered in the form. This is the role of a scripting language like PHP. This is just a very brief introduction to show the value of PHP scripting on your website.

The PHP scripting language (get it Here) will read, interpret and act on input from the user. Generally the php scirpt will output some to let you know it got the message and processed it. PHP has to be installed on the server hosting your website (see this). If you have a website being hosted or are considering it, make sure they support PHP or none of your php scripts will work (I'll capitalize PHP when I'm taking about the language itself and use lower case when referring to scripts).

Use the same editor to write your scripts that you use to edit HTML (I prefer vi, see this to see why). You need plain text, no proprietary formatting and no extra characters or symbols or anything else that might confuse php. PHP has excellent on-line help resources on their website so use those as you work with your scripts. Use indentation and comments foten and consistently so everything is easy to read. you'll come later to edit the script and you'll need these as hints to what you were doing and why.

You can name your scripts as some_script.php if they are stand-alone scripts. If the script is within a web page then all you do is use the <?php and ?> within the page. Whether this works depends on modifying the Apache httpd.conf file (see below).

A PHP script begins by declaring itself:

<?php
and then turns itself off with:
?>
This is the same techique you're familiar with from HTML and also used throughout the Apache configuration file (I'm going to assume you or your web host are using Apache, preferrably version 2). To get PHP to work with Apache you will need to edit the Apache configuration file, httpd.conf in the ServerRoot conf directory. This is not a big deal, just these changes:
LoadModule php5_module modules/libphp5.so

<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

AddType application/x-httpd-php .php .php3 .phtml .html

You might also want to add a directive to Apache, (ExecCGI), to tell it to execute php scripts in the top level directory for your web pages. This will allow you to run php scripts within a web page:
<Directory "D:/Apache2.2/htdocs">

Options Indexes FollowSymLinks Includes ExecCGI

The <Directory ... > line is your DocumentRoot, the place where you keep your web pages. You're telling Apache that it's ok to excute scripts inside the web pages in this directory. The specific installation for both Apache and PHP for your operating system is documented at the PHP web site. Make sure you follow those instructions to ensure that everything is working right (See the documentation Here).

A php script is mostly a collection of functions. They are words followed by parentheses:

function_name() { some php specific operations; }
These functions can manipulate user input in various ways and then display the results if you want. You can assign variables or use those created by PHP itself and there's lots of ways to display output. In most case you will display the script's output to a web page and PHP can create that page on-the-fly. PHP also creates a bunch of variables that you can use in your scripts. The phpinfo(); function will show all of them.

To create a php script:

<?php
$hello="Hello World!";
echo $hello;
phpinfo();
?>
Notice that the lines end with a semi-colon. I included the phpinfo(); line so you can also see how PHP sets its own variables. The echo command can be repaced by the print command but I prefer since I use it in shell scripts all the time. The usual way you'll call a script is through a web form which means you will be passing it the values the user filled in inside the form. The script will parse these values and decide how to use them based on whatever you put in that script.
<?php
echo "
<center>
<form action=\"/name.php\" method=\"post\" name=\"postit\">
<input type=\"hidden\" name=\"feedback\" value=\"some_value\">
<p>Enter your name here:
<input type=\"text\" name=\"name\" \">
<input type=\"submit\" value=\" Add you name \">
</form>";
?>
Because the echo command is being used with the double quotes (") you will have to escape any other double quotes with a backslash (\). The escaping of quotes is pretty much standard in all programming languages. Notice that the script also outputs a string of HTML code, this means that script both displays and interprets HTML. PHP can also call itself within an HTML document so that it prompts for input within a HTML form, acts on the input and displays HTML output all within the same page (Assuming that ExecCGI was enabled in the Apache httpd.conf file).

All the form elements, "name=" create variables. When the form is submitted those variables are passed to the php script where they are processed. Within the script you can massage these variables in all kinds of ways and then, if you want, store them in a file with whatever formatting you choose. This same script can also echo back some HTML code to verify that the input was accepted (or not).

You can format this feedback as a complete web page with CSS, tables, more forms or whatever you normally put in your web pages:

<?php
echo '

<html>
<head>
<title>
Feedback
</title>
<body>
<span style="font-family:verdana;font-size:11pt;">
<center>

<p>
<form action=comment.php method=post>

<table bgcolor="#eeeeff" cellspacing=10 border=5><tr><td>
<table background="images/cloth1.gif"><td><span style="font-family:verdana;">
.
.
.
Notice that this time I used the single quote ('). If you've got a lot of characters requiring escaping, use the single quote and avoid the hassle; it will escape (almost) everything for you. In this example an entire web page is being echoed back to the user's browser. This means that the page you display from the php script can be complete in every detail but with information specific to the current user. Since the page is generated when the script is run, it doesn't exist on your website and that means it takes up no disk space and it's one less file to upload or manage.

That's all I'm going to go into here since there's lots of really good tutorials on the Net already. My purpose here is only to show the virtues of PHP and how you can use it to make your website more useful. PHP is easy to learn, just start with a simple form and play with it.

Return to Contents Page