View Full Version : Need help with PHP Guestbook script

02-07-2010, 12:21 PM
I've managed to write a script for a simple php guestbook without database (it stores the comments into a simple file)
As I am beginner I used a video guide: http://www.youtube.com/watch?v=JtlRnUNfIj4

It seems that there are no compilation errors in the script.

<title>My Guestbook</title>

$file_name = "messages.txt";
$post_sep = "<>";
$info_sep = "&";

class writePosts {
function inputPost($author, $message){
global $file_name, $post_sep, $info_sep;
$date = time();
$self = $_SERVER['PHP_SELF'];
$author = urlencode($author);
$message = urlencode($message);
$fh = fopen($file_name, "r+");

fread($fh, filesize($file_name));
fwrite($fh, $author.$info_sep.$date.$info_sep.$message.$post_sep);

echo "<script>window.location = '$self'; </script>";

function getPost() {
if (isset ($_POST['author']) && isset($_POST['message'])){
$this->inputPost($_POST['author'], $_POST['message']);

class readPosts{
function readAll(){
global $file_name, $post_sep, $info_sep;
$fh = fopen($file_name, "r");
$all = fread($fh, filesize($file_name));
$each = explode($post_sep, $all);

$i = 0;
$counteach = count($each);
while($i< $counteach = 1){
$post_info = explode($info_sep, $each[$i]);
$this->outPost($post_info[0], $post_info[1], $post_info[2]);

function outPost($author, $date, $message){
$date = date("m/d/y", $date);
$author = urldecode($author);
$message = urldecode($message);

echo "<div>";
echo "( $date ) &nbsp; <strong$author says:</strong><br />";
echo "$message </div>";
$write = new writePosts;
$read = new readPosts;


<?php $read->readall(); ?>
<h1><div align="center">My Guestbook</div></</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="author" /_><br /_>
<textarea name="message"></textarea><br /_>
<input type="submit"value="Submit" /_>


So the issue is that when I try to launch the page with firefox or any other browser I get the following error.
Firefox can't find the file at /C:/wamp/www/$self.

I would really appreciate it if anybody could help me solve my issue.
Thank you in advance.

02-07-2010, 01:24 PM
Try removing

<?php echo $_SERVER['PHP_SELF']; ?> as it's not required
and also changing

echo "<script>window.location = '$self'; </script>";to

echo "<script type=\"text/javascript\">window.location = '".$_SERVER['REQUEST_URI']."'; </script>";

02-07-2010, 03:52 PM
Thanks. That helped and now it's working fine.

But I came across with another issue.
If someone writes a bigger comment, well I mean a really long one then it all shows in one row.
My question is how can I divide so it would not display it in one long row and would not have to scroll the browser sides to read it?