Sorry, I misled you a bit. Validation, though you should have that too, won't solve the problem. Plan B, cheating with JavaScript page redirection, will stop the problem by keeping the writing portion and the reading portion and seperate pages. Though it would be nice to fix the problem without resorting to JS, it can work for now. If you search around the 'net, you might be able to find a better solution. Here's what I did (you can find this working example at summers.dhs.org/scripts/php/test/guestbook.php):
-------- guestbook.php -----------
<head><title>The Guestbook!</title>
<body>
<?php
if ($page!=2) {
// login form
echo '
<form method="post" action="guestbook.php?page=2">
name <input size="25" name="name"><br>
email <input size="25" name="email"><br>
entry <input size="25" name="entry"><br>
<input type="submit" name="Submit" value="done">
</form>';
}
else {
// validate information
if (username!='') {
// write data to file
$filename = "guestbook.txt";
$guestbook = fopen($filename,"a+");
fwrite($guestbook,"\n");
fwrite($guestbook,$name);
fwrite($guestbook,"&%");
fwrite($guestbook,$email);
fwrite($guestbook,"&%");
fwrite($guestbook,$entry);
fclose($guestbook);
}
else {
echo ('You must enter a name!');
}
// JavaScript page change
echo "
<script language='JavaScript'>
<!--
window.location='guestbook2.php?name=$name&email=$email&entry=$entry'
--></script>";
}
?>
</body>
--------- guestbook2.php ---------
<body>
<?php
// read data from file and display
$filename = "guestbook.txt";
$guestbook = fopen($filename,"r");
$buffer = fgets($guestbook,4096);
while(!feof($guestbook)) {
$buffer = fgets($guestbook,4096);
list($name,$email,$entry) = split('&%',$buffer,3);
echo "<br>name: $name<br>";
echo "email: <a href='mailto:$email'>$email</a><br>";
echo "comment: $entry<br>";
}
fclose($guestbook);
?>
</body>