PHP File based guestbook, PHP Guestbook


 

PHP File based guestbook, PHP Guestbook

Here we have a single PHP script to add new records to the guestbook and also to show them on the page. It is a file based guestbook!

Here we have a single PHP script to add new records to the guestbook and also to show them on the page. It is a file based guestbook!

File based guestbook

In this tutorial we will create a file based PHP guestbook in this tutorial. I have decided to put everything into one executable file, and the full listing can be found in the bottom part of this text.
As always, let's start with HTML.
You can tune the HTML code as you want, but remember to leave field names as they are:


<FORM action=gb.php METHOD=POST>
Text: <TEXTAREA ROWS=4 COLS=40 name=text>ass</TEXTAREA><br>
Author: <INPUT TYPE=TEXT name=author><br>
Email: <INPUT TYPE=TEXT name=email><br>
Homepage: <INPUT TYPE=TEXT name=homepage><br>
<INPUT TYPE=SUBMIT>
</FORM>

See I wrote nothing special, just a set of the input types. And the method is POST cause we want user to post some data when he adds new message.

Now let me explain how the main script works.

The code shown below is to add new line to the file. Remember to use file locking functions if you want stable guestbook.

$fp=fopen($gbfile,'a+');
      flock($fp, LOCK_EX);
           fwrite($fp, $tmp. "\n");
      flock($fp, LOCK_UN);
fclose($fp);

Another important thing is to read the contents of a file:

$i=1;
while($i<$start && !feof($fp)) {
      $tmp=fgets($fp);
      $i++;
}
while($i<=$end && !feof($fp)) {
      $tmp=trim(fgets($fp));
      if ($tmp) { array_push($records, $tmp); }
      $i++;
}

The code above uses first "while" cycle to skip first lines and second "while" cycle reads the records to be shown. See how $start and $end variables are involved. These are for showing a certain number of records on the page.

Now I'll explain how the adding part works:

if ($REQUEST_METHOD=='POST' && $_POST['text']) {
      $msg = str_replace($separator, '', htmlspecialchars($_POST['text']));
      $author = str_replace($separator, '', htmlspecialchars($_POST['author']));
      $email = str_replace($separator, '', htmlspecialchars($_POST['email']));
      $homepage = str_replace($separator, '', htmlspecialchars($_POST['homepage']));

      $tmp = implode($separator, array($msg, $author, $email, $homepage));
      add($tmp);
}

This code checks if the form was submitted by POST method and then it adds all fields into the file as a string. Implode function is used to get single text line from a set of values.

I would not say much about the listing part and about the pagination code.

Just note this code:

?>
<HR>
Author: <?=$author?><br>
Email: <?=$email?><br>
Homepage: <?=$homepage?><br>
Text: <?=$msg?><br><br>
<?

It will output each record so you can write a better HTML code here.

Now I placed all this code into one file.
File gb.php
<?
$gbfile='gb.txt';
$separator= '^';

//====================================
//This function will add one line to
//the end of file
//====================================
function add($str){
global $gbfile;
      $tmp = trim($str);
      $fp=fopen($gbfile,'a+');
           flock($fp, LOCK_EX);
                fwrite($fp, $tmp. "\n");
           flock($fp, LOCK_UN);
      fclose($fp);
}

//====================================
//Function below gets specified number
//of lines and returns an array
//====================================
function get($start, $end){
global $gbfile;
      $records=array();
      $filename="gb.txt";
      $fp=fopen($gbfile,'r');
           flock($fp, LOCK_SH);
           $i=1;
           $tmp=TRUE;
           while($i<$start && !feof($fp)) {
                $tmp=fgets($fp);
                $i++;
           }
           while($i<=$end && !feof($fp)) {
                $tmp=trim(fgets($fp));
                if ($tmp) { array_push($records, $tmp); }
                $i++;
           }

           flock($fp, LOCK_UN);
      fclose($fp);
      return($records);
}

//=============================================
//Start of the script
//=============================================
//If the method is post then add new message
//to the guestbook file
if ($REQUEST_METHOD=='POST' && $_POST['text']) {
      $msg = str_replace($separator, '', htmlspecialchars($_POST['text']));
      $author = str_replace($separator, '', htmlspecialchars($_POST['author']));
      $email = str_replace($separator, '', htmlspecialchars($_POST['email']));
      $homepage = str_replace($separator, '', htmlspecialchars($_POST['homepage']));

      $tmp = implode($separator, array($msg, $author, $email, $homepage));
      add($tmp);
}

//Listing part

$start=$_GET['start'];
$end=$_GET['end'];

if (!$end || $start<=0) { $start=1; }
if (!$end) { $end=10; }
if ($end<$start) { $end=$start+1; }
$show=$end - $start;

//Get records from file into array
      $records = get($start, $end);

//For each record get each field
      foreach ($records as $rec) {
           $tmp = explode($separator, $rec);
           $msg = $tmp[0];
           $author = $tmp[1];
           $email = $tmp[2];
           $homepage = $tmp[3];

//=================================
//Outputting
?>
<HR>
Author: <?=$author?><br>
Email: <?=$email?><br>
Homepage: <?=$homepage?><br>
Text: <?=$msg?><br><br>
<?
      }

//Pagination
if ($start>$show) {
      $start-=$show;
      $end-=$show;
      print "<a href=gb.php?start=$start&end=$end>Prev $show</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

      if (count($records)!=0) {
      $start+=$show*2;
      $end+=$show*2;
      print "<a href=gb.php?start=$start&end=$end>Next $show</a>";
}
else {
      print "No more records found !";
}
}
else {
      $start+=$show;
      $end+=$show;
      print "<a href=gb.php?start=$start&end=$end>Next $show</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}
?>


How to use it...
First, remember to use HTML form that is shown in top part of this text. Second, to see the contents of the guestbook, you can create a regular link to the file:

<a href=gb.php>Guestbook</a>

If you want to have a certain number of records to be shown on each page of the guestbook, you can use this parameters for the script:
start: means number of the first record
end: means number of the last record
Example:
gb.php?start=1&end=10
If you want to configure this script with the design of your page you can insert any HTML code before the code (I mean before the first '<?'). And also you can add a footer after the last line of the file.

Ads