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> ";
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> ";
}
?>
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
ModuleNotFoundError: No module named 'guestbook'
ModuleNotFoundError: No module named '
guestbook' Hi,
My Python... '
guestbook'
How to remove the ModuleNotFoundError: No module named '
guestbook' error?
Thanks
Hi,
In your python environment you
php
php how to write a program in
php to create pdf
file
PHP
PHP hii,
Define
PHP?
Hello,ADS_TO_REPLACE_1
The
PHP... to create dynamic content that interacts with databases.
PHP is basically used for developing web
based software applications
ModuleNotFoundError: No module named 'zhouzhiqiang-guestbook'
ModuleNotFoundError: No module named 'zhouzhiqiang-
guestbook' Hi...: No module named 'zhouzhiqiang-
guestbook'
How to remove the ModuleNotFoundError: No module named 'zhouzhiqiang-
guestbook' error?
Thanks
Hi
ModuleNotFoundError: No module named 'aioli-guestbook'
ModuleNotFoundError: No module named 'aioli-
guestbook' Hi,
My... named 'aioli-
guestbook'
How to remove the ModuleNotFoundError: No module named 'aioli-
guestbook' error?
Thanks
Hi,
In your python
ModuleNotFoundError: No module named 'django-guestbook'
ModuleNotFoundError: No module named 'django-
guestbook' Hi,
My... named 'django-
guestbook'
How to remove the ModuleNotFoundError: No module named 'django-
guestbook' error?
Thanks
Hi,
In your
ModuleNotFoundError: No module named 'guestbook-duan'
ModuleNotFoundError: No module named '
guestbook-duan' Hi,
My... named '
guestbook-duan'
How to remove the ModuleNotFoundError: No module named '
guestbook-duan' error?
Thanks
Hi,
In your python
ModuleNotFoundError: No module named 'guestbook-pro'
ModuleNotFoundError: No module named '
guestbook-pro' Hi,
My... '
guestbook-pro'
How to remove the ModuleNotFoundError: No module named '
guestbook-pro' error?
Thanks
Hi,
In your python
ModuleNotFoundError: No module named 'jet-guestbook'
ModuleNotFoundError: No module named 'jet-
guestbook' Hi,
My... 'jet-
guestbook'
How to remove the ModuleNotFoundError: No module named 'jet-
guestbook' error?
Thanks
Hi,
In your python
PHP duplicate file - PHP
PHP duplicate
file i wanted to duplicate a
PHP file resides in different directory on submission
ModuleNotFoundError: No module named 'Rambo_zhou_guestbook'
ModuleNotFoundError: No module named 'Rambo_zhou_
guestbook' Hi...: No module named 'Rambo_zhou_
guestbook'
How to remove the ModuleNotFoundError: No module named 'Rambo_zhou_
guestbook' error?
Thanks
Hi
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 the
time a copy of ini
file places a copy of itself in /usr/local/lib/
php.
To find... in php.ini
file.
It should be keep in mind that if we use
PHP as CGI then we must
php download file script
php download
file script
PHP script to download
file in IE
php file search
php file search How to search
file in
PHP ..
$dir = '/tmp
What is PHP ?
software
To run a
PHP file we need following software:ADS_TO_REPLACE_6... features were included in
PHP 3 and improved in
PHP 4. In
PHP 5 many OOP
based... any other OOP
based programming language it included exception handling.
PHP 5
file_exists php not working
file_exists
php not working
file_exists
php not working. Please give me the simple example
PHP hide file path
PHP hide
file path
PHP to read a path and convert that to the virtual link
php parse ini file
php parse ini file I need a simple and easy example to Parsing an ?advanced? INI
file with
PHP
php download file code
php download
file code
PHP code to download the files from the remote server
change file permission in php
change
file permission in php How to change
file permission in
PHP
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
php
php what is
php
PHP Tutorials
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
PHP 5.4.23 Features
and easily. The Hypertext
Preprocessor or
PHP is basically
based upon the concept...;
Handling
file uploads: The latest version of
PHP
provides a number of ways...
PHP is the most popular open source, rich featured and high level web
php download file browser
php download
file browser limiting downloading
file system via browser only through my web application
php
php what is
php
Example of fgetc in PHP, fgetc php, fgetc in php
Learn how to use the fgetc() function in
PHP programming.
SyntaxADS_TO_REPLACE_1
string fgetc ( $
file_handler )
It gives a character by character by using the
file handler returned by fopen() and fsockopen() function
The string
PHP
PHP code into your html
file and save it as .
php or .php3
extensions. Then you...
PHP
PHP stands for Hypertext Preprocessor;
PHP
is server-side scripting language for development of web applications.
PHP
powerful and robust programming
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
php using javasript how to set timer in
php using javasript
The filegroup() function, filegroup php, filegroup() in php
of the given
file based on UNIX o/s
To convert it to proper format use the posix...
Example of filegroup() function in
PHP
Syntax of filegroup()in
PHP
int...
It doesn't work on window
Code for
php filegroup() Function
<?
php
echo
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 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
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 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
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
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 show the constructor overloading in
php
PHP
PHP How to work in drupal with
PHP
php download file from ftp
php download
file from ftp Need to download files from ftp using
php. a simple example
php download file from url
php download
file from url how to download multiple files in
PHP using the URL's
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 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
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 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 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 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
Related Tags for PHP File based guestbook, PHP Guestbook: