<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.
$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. 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. ?>
<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. <?
$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...<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: