PHP Hypertext Preprocessor GuestBook Tutorial
This tutorial shows how a guestbook might be created using PHP. The directions will help you to set up and manage your guestbook with little effort or intervention. The instructions are targeted to the construction of a guestbook that will reside within your site in it's own directory (to keep the content separate and manageable).
Creating the GuestBook
Create a web page for your guestbook named index.php and locate this page in a directory of it's own within your web site. You can use your web site design if you so choose. To this page you will add three parts in the following order: the processing code, the display code, and the submission code.
Processing code:
This code processes the submission, modifies the associated files, and e-mails the guestbook owner after every new entry. It is placed before the display code.
- a link down to the form for a new visitor,
- a thank you for a new submission, and
- a message that sequential submissions are not permitted by the same visitor.
Display code:
This call will display the current information found in the guestbook. The code must be after the [processing code] in order to display the current changes to the guestbook.
Display Note:
Replace the occurrence in the file that reads PATH_TO_YOUR_GUESTBOOK_DIRECTORY with the correct path to these files.
Example: The path to this demo guestbook is:
/www/tutorial/guestbook/gb
Submission code:
The submission code is a fairly standard web form that simply calls the same page where the form initially was found. In calling itself, the form executes the associated PHP code to process the submission and redisplay the result. This section is generally placed near the end of the guestbook page so that a visitor may read prior submissions before they submit their own feedback. The PHP code in this section will automatically insert the address of the current web page so that the form will work properly.
Submission Note:
If you need to modify this form to ask for other information you will also need to modify the PHP of the [processing code] for that information to be used and saved.
Creating the Associated Files
Two text files are needed to store messages posted to your guestbook:
- GB-current.php
- 'Current' postings to display will be found in this file.
- GB-archive.php
- This file will contain the postings for 'Archive' purposes.
The modules in this PHP tutorial will automatically create these two files for you if they are not found. The files will be created in the same directory where your guestbook is located.
Read/Write permission:
With the PHP version of the guestbook it will not be necessary to give the server access to the files. The server will have created the files and should already have access.
However, since the server may own the files you may need access to the files yourself. It may be necessary for you to enlist the aid of your system administrator if you need to be able to read and edit these files directly.
Securing Your GuestBook with File Access Control
Caution: Using this security feature may prevent some users from accessing your guestbook. The method explained will also only work on an Apache web server. The reason for this is the feature checks to see where the visitor was when they requested the guestbook page (the referring web address).
Some Internet providers do not forward referring address information (i.e. AOL). Because of this those visitors will never be able to view your guestbook. You must weigh your options: 1) let any referrer call your guestbook, thereby bypassing your site entirely; 2) restrict referrers and in the process loose some possible feedback.
One additional file is needed to ensure that the entry to your guestbook is only made from your web site. Your guestbook directory needs an access control file in the name of .htaccess to control who can call your guestbook. This file is placed in your guestbook directory.
HTACCESS Note:
In this file replace PATH_TO_YOUR_WEB_SITE with the correct path to your web site (not to your guestbook directory), and replace http://WEB_SERVER_ADDRESS/ with your web server's address.
| /tutorial/guestbook/ |
|
http://www.melvinwallerjr.com/ |
| http://www.melvinwallerjr.com/tutorial/guestbook/ |
If you have this set up correctly, links from your web site should display your guestbook. However, links from other sites, browser bookmarks, or entering the address directly in the browser should result in another page within your web site. (Be sure to reload your page to verify this works. Some browsers will cache the guestbook page and not show the true results.)
Finishing Up
You should now have created one directory (located within your web site) which contains up to four files. These are: the guestbook web page, the current postings file, the archive postings file, and the access control file. All that is needed now is to link to your guestbook and test it.
PHP GuestBook Demo
Finally, now that you have gone through the steps of how to build the guestbook, take a look at a PHP GuestBook working example.
