Trying to prevent form re-submission upon refresh, can't modify header information
I'm trying to make the page redirect to the same page to avoid resending the same form information. However, using header('Location: guestbook.php');
gives me an error:
Warning: Cannot modify header information - headers already sent by (output started at /test/guestbook.php:1) in /test/guestbook.php on line 29
I'm not sure if I'm putting the header in the right 开发者_开发问答place, I'm not extremely familiar with using it:
<?php
$gb_str = ""; // $gb_str is the string we'll append entries to
$pgeTitle = "View and Sign Guestbook";
// If form is submitted, then insert into DB
if (!empty($HTTP_POST_VARS["submit"])) {
// initiate some vars
$dbHost = ;
$dbUser = ;
$dbPass = ;
$dbDatabase = ;
$li = mysql_connect($dbHost, $dbUser, $dbPass) or die("Could not connect");
mysql_select_db($dbDatabase, $li) or die ("could not select DB");
$name = mysql_real_escape_string($HTTP_POST_VARS["name"]);
$email = mysql_real_escape_string($HTTP_POST_VARS["email"]);
$comment = mysql_real_escape_string($HTTP_POST_VARS["comment"]);
$date = Date("Y-m-d h:i:s");
$gb_query = "insert into entries
values(0, '$name', '$email', '$comment', '$date')";
mysql_query($gb_query);
$res = mysql_affected_rows();
// See if insert was successful or not
if($res > 0) {
$ret_str="Your guestbook entry was successfully added!";
header('Location: guestbook.php');
exit(); // End the request
} else {
$ret_str = "There was a problem with your guestbook entry. Please try again.";
}
// Append success/failure message
$gb_str .= "<span class=\"ret\">$ret_str</span><BR>";
mysql_close();
}
?>
You have BOM or just regular spaces in the beginning of the file. Just remove them
Most probably you have sent output before setting header();
.
Try disabling notices (and probably warnings) and make sure you don't send any output before setting header();
.
<?php
error_reporting(E_ALL ^ E_NOTICE); // Print all errors except notices - they come out for example when you're requiring an undefined var.
$gb_str = ""; // $gb_str is the string we'll append entries to
$pgeTitle = "View and Sign Guestbook";
// If form is submitted, then insert into DB
if (!empty($HTTP_POST_VARS["submit"])) {
// initiate some vars
$dbHost = "localhost";
$dbUser = "someuser";
$dbPass = "password";
$dbDatabase = "database";
$li = mysql_connect($dbHost, $dbUser, $dbPass) or die("Could not connect");
mysql_select_db($dbDatabase, $li) or die ("could not select DB");
$name = mysql_real_escape_string($HTTP_POST_VARS["name"]);
$email = mysql_real_escape_string($HTTP_POST_VARS["email"]);
$comment = mysql_real_escape_string($HTTP_POST_VARS["comment"]);
$date = Date("Y-m-d h:i:s");
$gb_query = "insert into entries values(0, '$name', '$email', '$comment', '$date')";
mysql_query($gb_query);
$res = mysql_affected_rows();
// See if insert was successful or not
if($res > 0) {
$ret_str="Your guestbook entry was successfully added!";
header('Location: guestbook.php');
exit(); // End the request
} else {
$ret_str = "There was a problem with your guestbook entry. Please try again.";
}
// Append success/failure message
$gb_str .= "<span class=\"ret\">$ret_str</span><BR>";
mysql_close();
}
?>
精彩评论