PHP Guestbook Script

Written By: Nick Tiberi

- 08 May 2006 -
















Description: In this tutorial we will create a simple but functional guestbook using PHP. All you need is a text editor and a web host that supports PHP. No MySQL is needed.

  1. Create the Form
  2. Handle the Data
  3. View Data Only

Handle the Data

Now we want to actually do something with the data inputted into the form. That's where the "guestbook.php" file comes in. You can include the same CSS file in this file as well. Here comes the part where we actually use some PHP!


 
$name = $_POST['Name'];        
$email = $_POST['Email'];              
$comments = $_POST['Comments'];
$timedate = date("M d, Y (g:i a)", time() + 7200);
 
if ($name == "" or $comments == "") {
        echo "One or more required fields were not completed. Please go back and try again. 

"
; } else { $oldinfo = file_get_contents("data.txt"); $guestbookdata = fopen("data.txt", "w+"); fwrite($guestbookdata, "
\"post\">Name: \"mailto:{$email}\">{$name} - {$timedate}
\n"); fwrite($guestbookdata, "Comments: $comments
\n \n"
); fwrite($guestbookdata, "$oldinfo"); fclose($guestbookdata); readfile("data.txt"); } ?>

Look a little scary? Don't worry! Let's take a look at this line-by-line.

$name = $_POST['Name'];         
$email = $_POST['Email'];              
$comments = $_POST[\Comments']; 

These three lines serve the same function: they take the data from our form and store it in three separate variables — namely, the $name, $email, and $comments variables. Not so bad, huh?

$timedate = date("M d, Y (g:i a)", time() + 7200);

This line stores the date and time that the post was made in the $timedate variable. The cryptic code you see is as follows:

     M – month spelled out
     d – day of the month
     Y – year
     g – hour of the day
     i – minutes
     a – am/pm

I also added the time() + 7200 because I'm in EST. You might need to adjust this value depending on where you live.

if ($name == "" or $comments == "") {
        echo "One or more required fields were not completed. Please go back and try again. 

"
;

This stuff just checks to see if either the name or comments fields were left blank. If they were, it prompts the user with the message telling them that they didn't complete all of the required fields.

else {
        $oldinfo = file_get_contents("data.txt");

The "else" says that if all the required fields were completed, then go to the next set of statements. The next statement stores the contents of the file "data.txt" (which you also need to create — it's just a blank text file) in the $oldinfo variable.

$guestbookdata = fopen("data.txt", "w+");

This line opens the data.txt file and totally clears everything in the file. That's why we needed to get the information from the file first!

fwrite($guestbookdata, "
\"post\">Name: \"mailto:{$email}\">{$name} - {$timedate}
\n"); fwrite($guestbookdata, "Comments: $comments
\n \n"
);

These lines simply write the XHTML needed for a new entry in the text file (data.txt).

fwrite($guestbookdata, "$oldinfo");

This line writes all of the previous entries to the data.txt file.

fclose($guestbookdata);
readfile("data.txt");

These lines close the file and read the contents of the file, respectively. Reading the file will display the XHTML that was just created as well as any other previous entries that were there.

<< Previous

Next >>