试图阻止表单重新提交刷新,无法修改标题信息

时间:2011-07-09 04:19:24

标签: php

我正在尝试将页面重定向到同一页面,以避免重新发送相同的表单信息。但是,使用header('Location: guestbook.php');会给我一个错误:

警告:无法修改标题信息 - 第29行/test/guestbook.php中已经发送的标题(在/test/guestbook.php:1处开始输出)

我不确定我是否将标题放在正确的位置,我对使用它并不是非常熟悉:

<?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();
}
?>

2 个答案:

答案 0 :(得分:4)

您在文件的开头有BOM或只是常规空格。只需删除它们

答案 1 :(得分:1)

很可能您在设置header();之前已经发送了输出 尝试禁用通知(可能还有警告)并确保在设置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();
}
?>