Introduction
If you are a great fan of PHP, you might have installed PHP on an Apache server and used MySQL as the backend on windows or a linux machine. Most of the people will develop and test their code on a windows machine till they move their work on a production machine.
In this article I am going to step you through installing PHP under IIS (Internet Information Server) on Windows 2000 and use Microsoft Access as backend (in fact you could use Foxpro, SQL Server, Interbase or any other ODBC database).
Installing PHP
Download Windows binary for the latest version of PHP. Once you've downloaded it, unzip it into a C:\, rename the folder to "php".
Open the PHP.INI-DIST file from C:\PHP folder, locate the following line:
;cgi.force_redirect = 1
We need to uncomment this line and change the setting to 0 as shown below to run PHP under IIS, do the necessary changes and save the file as php.ini in your windows folder(C:\WINNT on my computer).
cgi.force_redirect = 0
Configuring IIS for PHP
We shall assume in this section that your IIS is up and running, click Start-Setting-Control Panel-Administrative Tools- Internet Service Manager
Select Default Web Site, right click and select property...
Select the Home Directory Tab and click Configuration
In the Application Configuration window select App Mappings and click Add Button
Enter the executable as C:\PHP\PHP.EXE (or the location where you have installed PHP) and the extension as .php as shown in the above figure and click OK. This configures IIS to recognize and run PHP Files with PHP interpreter.
Open your favorite text editor and create a text file and type the following in it
<?php
phpinfo();
?>
phpinfo() is a PHP function which outputs a large amount of information about the current state of PHP. This includes information about PHP compilation options and extensions, the PHP version, server information and environment, the PHP environment, OS version information, paths, master and local values of configuration options, HTTP headers, and the PHP License.
Save this file as test.php in C:\INETPUB\WWWROOT (or your IIS Web Root Folder)
Open you browser and type http://localhost/test.php in the address bar, click Go to see if you see output like the screen below to make sure PHP in configured and running properly.
If the source code appears in the browser and not the output we are expecting, then PHP has not been properly installed or IIS does not recognizes PHP files, please make sure you have followed the instructions properly.
Creating Access Database and DSN
Create a Microsoft Access Database with the following structure
code Autonumber
parentcode Number
title Text
description Text
uname Text
email Text
Code is a unique, autonumber field, which shall identify each thread in the discussion forum, parentcode is the field, which is linked to code field to specify the child thread of any thread, parentcode will be 0 for all the main threads.
Title, description, uname and email fields are used to save more information on each thread. This Structure gives us the flexibility to create a tree structure of thread and child thread for n levels.
To create a DSN for this database, click Start-Setting-Control Panel-Administrative Tools and open Data Sources (ODBC)
Click System DSN tab and click add, select Microsoft Access Driver (*.mdb), type forum as the Data Source Name and select the database you have just created Click Ok to create this DSN.
Creating PHP Files
We will have to create two PHP files (forum.php & node.php) to make our discussion forum work.
Please open any text editor and create the two files with the source code below (the code is well documented to tell you what each line does), save this file in C:\INETPUB\WWWROOT folder
Here is the code for first PHP file (forum.php)
<?php
// This is the DSN we have create for our database
$connect = odbc_connect("forum", "root", "");
?>
<html>
<body>
Discussion Forum using PHP/Access under IIS<br /><br />
<a href="node.php?node=0">Post New Message</a>
<?php
shownode(0); // display all the main threads
// This function is a recursive function which shall display all the br /anches
// and sub br /anches of the threads
function shownode($nodecode)
{
global $connect; // using the global variable for connection
// Get a list of all the sub nodes which specific parentcode
$noderesult = odbc_exec($connect,"select * from forum where parentcode = $nodecode");
echo '<ul type="disc">';
while(odbc_fetch_row($noderesult)) // get all the rows
{
$code = odbc_result($noderesult,"code");
$title = odbc_result($noderesult,"title");
$uname = odbc_result($noderesult,"uname");
$email = odbc_result($noderesult,"email");
echo "<li>";
echo "<a href=\"node.php?node=$code\"> $title </a>";
echo "-- by ($uname) $email<br />";
shownode($code);
}
echo "</ul>";
}
?>
</body>
</html>
This is the code for the second PHP file (node.php) file.
<?php
$connect = odbc_connect("forum", "root", "");
if(isset($submit)) // check if submitted button is clicked
{
// insert the record in the database
$resultupdate=odbc_exec($connect,"insert into forum
(parentcode,title,description,uname,email) VALUES
($node,'$title','$description','$postname','$email')");
header("location:forum.php"); // open forum.php file to display the thread
exit;
}
?>
<div align="center">Post to Discussion Forum using PHP/Access under IIS</div>
<?
if ( $node != 0 )
{
// Displaying the details of the thread
echo "<hr />";
$noderesult = odbc_exec($connect,"select * from forum where code = $node");
$noderow=odbc_fetch_row($noderesult);
$title = odbc_result($noderesult,"title");
$description = odbc_result($noderesult,"description");
$uname = odbc_result($noderesult,"uname");
$email = odbc_result($noderesult,"email");
echo "$title by ($uname) $email<br />";
echo "$description <br /><hr />";
}
?>
<!-- Form to enter the message -->
<form method="post">
Name : <input type="text" name="postname"></input><br />
E-Mail : <input type="text" name="email"></input><br />
Title : <input type="text" name="title" value="" size="50"></input><br />
Description : <br /><textarea name="description" rows="10" cols="45"></textarea>
<!-- we need a hidden field to store the node -->
<input type="hidden" name="node" value="<?php echo $node;?>"><br />
<input type="submit" name="submit" value="Post Message">
</form>
Finally
Now its time for us to run the PHP files and see the discussion forum in action please open the br /owser and type http://localhost/forum.php click Post New Message and create few messages and replies to see how our discussion forum works. Here is the output of the screen on my computer with some test discussion in it.
Conclusion
By now you must be convinced that PHP is very versatile and works with any platform, web server and database, also it is very easy to create a discussion forum with just one table and two PHP Files.
--Jayesh is an applications consultant for a health company in Auckland, New Zealand. He has several years of n-Tier development experience and is currently working with Visual Basic.NET to develop interactive client solutions.