...

View Full Version : Guestbook repost when refresh



theprophet
12-21-2004, 03:07 AM
The problem I am having is when I post to my guestbook I get what I put in but if i refresh the page it sends the information again. So what I want to figure out is how to prevent this. Here is a example of my code


<?php


$name = $_GET["txt_name"];
$len = strlen($name);
if($len > 0)
{
$email = $_GET["txt_email"];
$comment = $_GET["txt_comment"];
$date = time();

$query = "INSERT INTO guestbook (autoID, name, email, comment, date_auto)
VALUES (NULL, '$name', '$email', '$comment', '$date')";

mysql_query($query, $connection) or die (mysql_error());
}

?>
<html>
<head>
<title>Welcome to Keiths Guestbook</title>
</head>
<body>
<center>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="GET">
<font face="arial" size="1">
Name: <input type="text" name="txt_name">&nbsp;
Email: <input type="text" name="txt_email"><br><br>
Comment: <br>
<textarea style="width: 75%" rows="10" name="txt_comment"></textarea>
<center><input type="submit" value="Submit"></center>
</font>

<table bgcolor="#AAAAAA" border="0" width="75%" cellapceing="1" cellpading="2">
<?php
$query = "Select * FROM guestbook ORDER BY date_auto";
$result = mysql_query($query, $connection);

for ($i = 0; $i < mysql_num_rows($result); $i++)
{
$name = mysql_result($result, $i, "name");
$email = mysql_result($result, $i, "email");
$email_len = strlen($email);
$comment = mysql_result($result, $i, "comment");
$comment = nl2br($comment);
$date = mysql_result($result, $i, "date_auto");
$show_date = date("H:i:s m/d/Y", $date);

if ($i % 2)
{
$bg_color="#EEEEEE";
}
else
{
$bg_color="#E0E0E0";
}

echo '
<tr>
<td width="100%" bgcolor="'.$bg_color.'">
<font face="arial" size="2">';
if ($email_len > 0)
{
echo '<b>Name:</b> <a href="mailto:'.$email.'">'.$name.'</a>';
}
else
{
echo '<b>Name:</b>'.$name;
}
echo '
<br>
<b>Comment:</b> '.$comment.'
</font>
<td>
<td width="1%" valign="top" nowrap bgcolor="'.$bg_color.'">
<font face="arial" size="2">
<b>Date:</b> '.$show_date.'
</font>
</td>
</tr>
';
}
?>

</table>

</center
</body>
</html>

xiaodao
12-21-2004, 03:17 AM
i am a newbie, this is my try


if (!isset($registered)) {
setcookie('registered','true',time()+300);//limit 5 mininutes
$query = "INSERT INTO guestbook (autoID, name, email, comment, date_auto)
VALUES (NULL, '$name', '$email', '$comment', '$date')";
} else {
echo "registered, dont submit twice!";
}

and, i suggest you do some validation to kick out html codes and special chars.

4xz
12-21-2004, 07:01 PM
Change the top part to :


<?php


$name = $_GET["txt_name"];
$len = strlen($name);
if($len > 0)
{
$email = $_GET["txt_email"];
$comment = $_GET["txt_comment"];
$date = time();

$query = "INSERT INTO guestbook (autoID, name, email, comment, date_auto)
VALUES (NULL, '$name', '$email', '$comment', '$date')";
$result= mysql_query($query, $connection);
if($result) {
header("Location: guestbook.php?alert=Entry%20added%20to%20guestbook");
} else {
die (mysql_error());

}

?>
....


and put this somewhere in the BODY of the page :



<?php
if(isset($_GET[alert]) {
echo "<h1>".$_GET[alert]."</h1>";
}
?>


The trick is to redirect the user that has just posted the entry to a 'fresh' guestbook, without the request parameters (in this case $_GET["txt_name"]).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum