<< 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.

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 />


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


$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)
fputs ($fp, " $date1, <a href='mailto:$email'>$name</a>: $txt <hr>


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";

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

session_unset ();
session_destroy ();

[!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