Ok I finally solved the issue. Here I want to share my working guestbook code.
<html>
<head>
<title>GuestBook</title>
<style type = "text/css">
body{
background: tan;
}
</style>
</head>
<body>
<center>
<h1>Sample Guestbook</h1>
<table border = 1
width = 500>
<tr>
<td colspan = 2> <center>
<h3>Comments</h3>
</td>
</tr>
<tr>
<?php
$userName=$_POST["userName"];
$content=$_POST["content"];
if($userName=="")
{
} else {
post();
}
showComments();
function post()
{
global $commentFile, $userName, $content;
$commentFile = "comment.log";
$fp = fopen($commentFile, "a");
$data="";
$today = date ("F j, Y, g:i a");
$data .= $today;
$data .= "+";
$location = getenv("REMOTE_ADDR");
$data .= $location;
$data .= "+";
$data .= $userName;
$data .="+";
$data .=$content;
$data .="\n";
fputs($fp, $data);
fclose($fp);
}
function showComments() {
global $commentFile;
$commentFile = "comment.log";
$fp = fopen($commentFile, "r");
while(!feof($fp))
{
$comments .= fgets($fp);
}
fclose($fp);
if ($comments == NULL)
{
print <<<HERE
<tr>
<td colspan=2>
No comments yet!
$says
</td>
</tr>
HERE;
} else {
$comments = split("\n", $comments);
foreach($comments as $cur) {
list($date, $ip, $user, $says) = explode("+",$cur);
if ($says == "" || $user=="")
{
} else {
print <<<HERE
<tr>
<td >
<font color="blue">Date: $date </font><br>
IP: $ip <br>
<font color="red">$user</font> says
$says
</td>
</tr>
HERE;
}
}
}
}
?>
<td><center>
<form action="guest.php" method="post">
Name <input type="text" name ="userName" value="">
<textarea name="content" rows=20
cols=40>Enter your comment here!</textarea><br>
<input type="submit" value="Post"></form>
</center></td>
</tr>
</table>
</center>
</body>
</html>