<< back | print

For that simple guestbook you need 3 files:
- show.php (its the main page and can/must be included to the homepage)
- write.php (this file writes the entries from the formular in show.php to a textfile)
- text.htm (this file contains the messages)

Spam blocker: Every time you visit the show.php (the Guestbook) you 'll see a random generated number (I call it Security code).
The value of the field 'Code' must be the same as the generated number, otherwise the formular won't sent.

show.php
=========
<?php
session_start(); // start the Session
$zufall = rand(1000,9999); // make a random number
$_SESSION['zufallszahl'] = $zufall; // save random number in Session

// get old guestbook entries
$messagepfad ="text.htm";
$tmp = "$messagepfad";
$output = @array_reverse(file("$tmp"));
?>

Security code: <? echo $zufall; ?> <br />

<form action='write.php' method='post'>
Name: <input type='text' name='name' value='' maxlength='25'><br />
Email: <input type='text' name='email' value='' maxlength='30'><br />
Text: <input type='text' name='txt' value='' size='60'> <br />
Code: <input type='text' name='seco' size='10' value='' maxlength='6'>
<input type='hidden' name="PHPSESSID" value="<?=session_id()?>">

<INPUT TYPE='submit' name='submit' VALUE='OK'><p />
</form>

<hr>

<?php
// show the guestbook/text entries
echo @implode('', $output);
?>

write.php
=========
<?php
session_start();

$seco = $_POST['seco'];
if(isset($_SESSION['zufallszahl']) AND $_SESSION['zufallszahl'] == $seco)
{
$fp = fopen("text.htm","a"); // create the text file
$date1 = date("d.m.y");

if ($fp)
{
flock($fp,2);
fputs ($fp, " $date1, <a href='mailto:$email'>$name</a>: $txt <hr>
");

flock($fp,3);
fclose($fp);

echo "<p> Thanks for your entry! <a href='show.php'>BACK TO HOMEPAGE</a></p>";
echo "<hr><a href='mailto:$email'>$name</a> $date1 : $txt";
}
}

else
{
echo "Please enter the correct security code. <a href='show.php'>BACK</a>";
}


session_unset ();
session_destroy ();
?>

text.htm
=========
[!emtpy! , make sure that chmod to that empty file is set to 777]

the entries later look like this:
23.02.06, <a href='mailto:--'>Test</a>: test tet test test <hr>
23.02.06, <a href='mailto:dfgh'>fgh</a>: sdfg <hr>
23.02.06, <a href='mailto:dfgh'>fgh</a>: sdfg <hr>

Many thanks to "niagara" from spotlight.de