Home Techindex PHP File based guestbook, PHP Guestbook

 
 

PHP File based guestbook, PHP Guestbook
Posted on: April 14, 2006 at 12:00 AM
Advertisement
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.
More Tutorials on roseindia.net for the topic PHP File based guestbook, PHP Guestbook.
PHP File based guestbook, PHP 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... to use file locking functions if you want stable guestbook. $fp=fopen
 
php
php  how to write a program in php to create pdf file
 
PHP
PHP   hii, Define PHP?   Hello, The PHP Hypertext... content that interacts with databases. PHP is basically used for developing web based software applications
 
PHP duplicate file - PHP
PHP duplicate file   i wanted to duplicate a PHP file resides in different directory on submission
 
php file search
php file search  How to search file in PHP .. $dir = '/tmp
 
php download file script
php download file script  PHP script to download file in IE
 
PHP error uploading file - PHP
PHP error uploading file  I am getting error while uploading a file in a folder in PHP ... Warning: Cannot modify header information - headers already send any idea
 
PHP ini
customize the behavior of php. Using this file maintaining PHP will be as easy... of ini file places a copy of itself in /usr/local/lib/php. To find out the ini... search option from start menu. php.ini file allows to customize PHP
 
PHP File Manipulation File locking Tutorial
PHP File locking Learn how to use PHP file locking. You need to lock the file each time when you do any input/output operation with that file... to a guestbook in one time. To avoid this, you should use file locking procedure
 
What is PHP ?
software   To run a PHP file we need following software:  i) Web Server.... In PHP 5 many OOP based features are included like private, public member...What is PHP? PHP is an acronym stand for Hypertext PreProcessor. It is an open
 
file_exists php not working
file_exists php not working  file_exists php not working. Please give me the simple example
 
change file permission in php
change file permission in php  How to change file permission in PHP
 
php download file code
php download file code  PHP code to download the files from the remote server
 
php parse ini file
php parse ini file  I need a simple and easy example to Parsing an ?advanced? INI file with PHP
 
PHP hide file path
PHP hide file path  PHP to read a path and convert that to the virtual link
 
php search file
php search file   I wanted to write a php script to list all the files available in either folder or sub folder
 
Example of fgetc in PHP, fgetc php, fgetc in php
used to display the content of the file Some functions based on character ... Learn how to use the fgetc() function in PHP programming. Syntax string fgetc ( $file_handler ) It gives a character by character by using the file
 
php download file browser
php download file browser  limiting downloading file system via browser only through my web application
 
Multiple File Upload in PHP
Multiple File Upload in PHP  Hi, I am beginner in PHP scripting language. I am very interested to learn PHP application. So, can anyone explain or provide related reference about how to Multiple file upload in PHP. Thanks
 
Import MYSQL file in PHP - PHP
Import MYSQL file in PHP  i wanted to import the mysql file in PHP5 on button click. is it possible to do it? Is there any possibility of data security
 
PHP Based Chatserver - Design concepts & design patterns
PHP Based Chatserver  Hey i want to create a Chat server like this one : www.scn-chat.com could someone plz tell me were can i get a chatserver
 
PHP 5.4.23 Features
and easily. The Hypertext Preprocessor or PHP is basically based upon the concept...PHP is the most popular open source, rich featured and high level web... servers. The PHP (Hypertext Preprocessor) scripting language is widely used
 
The filegroup() function, filegroup php, filegroup() in php
Example of filegroup() function in PHP Syntax of filegroup()in PHP int filegroup ( filename) It gives the groupid in integer format of the given file based on UNIX o/s To convert it to proper format use the posix_getgrgid
 
php
php   what is php   PHP Tutorials
 
Read external file in PHP
Read external file in PHP  How to read external files in PHP in particular time duration and save that file with the latest date. How is it possible? Please explain with an example. Thanks
 
php
php  what is php
 
PHP
You can embedded PHP code into your html file and save it as .php or .php3...PHP PHP stands for Hypertext Preprocessor; PHP is server-side scripting language for development of web applications. PHP powerful and robust programming
 
PHP Introduction
file. So, you can make your html dynamic by using the PHP code in it. Generally PHP file is saved with the .php extension. Since, PHP is interpreted language... PHP program. PHP Excel Reader Read the xls file and retrieves the data
 
PHP Open Excel File in browser - PHP
PHP Open Excel File in browser  How we can open an excel file using PHP code. I don?t want to let the user to download it rather I would like to get it open in the web browser. Any help would be great. Thank you very much
 
php download file from url
php download file from url  how to download multiple files in PHP using the URL's
 
php download file from ftp
php download file from ftp  Need to download files from ftp using php. a simple example
 
php
php using javasript  how to set timer in php using javasript
 
What's PHP ?
developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications. Basic syntax: A PHP...What's PHP ?  What's PHP ?   Hi friends, PHP stands
 
php
php  i want to know about php and php plateform required software to make php program and database that used in php program?   Please visit the following link: PHP database
 
php
php  i want to know about php and php plateform required software to make php program and database that used in php program?   Please visit the following link: PHP database
 
PHP
PHP  How to work in drupal with PHP
 
php
php  show the constructor overloading in php
 
PHP Comma vs Period - PHP
PHP Comma vs Period  Explain PHP Comma vs Period. And also how can one show a comma within a comma separated file.   Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/php/phpbasics/PHP
 
The file function example in PHP, file php, file in php
In this php tutorial section we will be learning the file() function of PHP...() function in PHP array file ( filename, [,flags [, context]] ) It returns...; the context  Code for file() in PHP <?php $ar1=file("c
 
PHP saying folder does not exist
PHP saying folder does not exist  This PHP code is supposed to check if a folder exists based on the value of the URL perimeter ?username= <?php...://officedocs.freeserver.me/userdata/" . $username . "/docs.xml"; if (file_exists($filename
 
php
php  what is php   PHP is one of the most used open source server-side scripting language, which runs on almost all the platform. PHP... and Solaris. To know more about PHP, Visit Here
 
PHP file_get_contents() Function
PHP file_get_contents() Function  Hi, How do i read the content of file using PHP program? So kindly provide any example or online help reference for filegetcontents() Function in PHP. Please feel free to suggest. Thanks
 
zip file upload in php - WebSevices
zip file upload in php  This is not a normal upload. we know the code for normal upload. i need the zip file upload code in php. how to upload zipfile using php? and how to unzip using php? please i dont
 
PHP webdesign
PHP webdesign  HI .. I want create a website using fully PHP(Not in Html). I have separate file like header.PHP,body.PHP, Footer.PHP... How to integrate these all files in word press
 
php java
php java  why PHP is becoming more popular than java   PHP is more popular than Java because of following reasons: 1)PHP web hosting options are superior. 2)PHP provides rapid development and instant gratification
 
php
php  retrieve data from mysql in php   Please visit the following links: http://www.roseindia.net/sql/mysql-table/mysql-php-select.shtml http://www.roseindia.net/sql/mysql-example/select-where.shtml
 
php
php  plz tell me code for PHP SQL Insert,delete,update,view is used to insert the record from HTML page to Mysql database using in single PHP form
 
PHP
PHP  How to check whether a username exists on a website using php and cURL and not MySQL
 
PHP
PHP  I want to create Calendar Event in PHP. Could u help me
 
php
php  how to set email remainder in php?can you help me pls
 
Advertisement

Related Tags for PHP File based guestbook, PHP Guestbook:


Advertisements Advertisements
 

 

 

DMCA.com