“警告:无法修改标题信息 - 已由[错误]发送的标题”

时间:2012-11-28 19:37:50

标签: php mysql header

  

可能重复:
  “Warning: Cannot modify header information - headers already sent by” error

这是我的代码:

$con = mysql_connect("localhost",$dbuser,$dbpass);
mysql_select_db($dbname, $con);
function crSalt($max = 15) {
        $characterList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $i = 0;
        $salt = "";
        while ($i < $max) {
            $salt .= $characterList{mt_rand(0, (strlen($characterList) - 1))};
            $i++;
        }
        return $salt;
}
if ($_POST['username'] && $_POST['password']){
$username=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$hash = hash("ripemd160", $password);
$x = mysql_fetch_array(mysql_query("SELECT * FROM `accounts` WHERE `Username`='$username'"));
if ($hash == $x['Password']){
$salt = crSalt();
setcookie("xtx_SALT", $salt, time()+3600*74 , "/");
setcookie("xtx_ID", $x['id'], time()+3600*74 , "/");
setcookie("xtx_CRED", hash("ripemd160", $username), time()+3600*74, "/");
mysql_query("UPDATE `accounts` SET `Salt`='$salt' WHERE `Username`='$username'");
}
}
?>
<form action="" method="post">
Username: <input type="text" name="username" /><br/>
Password: <input type="password" name="password" /><br/>
<input type="submit" value="Login!" />
<form>

我一直在努力解决这些错误:

  

警告:无法修改标头信息 - 已发送的标头   (输出始于   /home/xtrosx10/public_html/Secure/Login/index.php:5)in   第27行/home/xtrosx10/public_html/Secure/Login/index.php

     

警告:无法修改标头信息 - 已发送的标头   (输出始于   /home/xtrosx10/public_html/Secure/Login/index.php:5)in   第28行/home/xtrosx10/public_html/Secure/Login/index.php

     

警告:无法修改标头信息 - 已发送的标头   (输出始于   /home/xtrosx10/public_html/Secure/Login/index.php:5)in   第29行/home/xtrosx10/public_html/Secure/Login/index.php

我不明白为什么我实际上得到这个,因为这里没有一行代码表明应该发送标题。

2 个答案:

答案 0 :(得分:2)

setcookie会尝试修改标头,您需要确保在该点之前没有echo或错误/注意/警告部分代码。尝试查看错误日志。

setcookie:http://php.net/manual/en/function.setcookie.php

答案 1 :(得分:0)

输出的可能原因是引发并打印出PHP E_NOTICE错误 - 可能在访问不存在的数组索引时。

这些警告会告诉您行号,因此请检查是否有可能在那里引发E_NOTICE。

禁用display_errors并启用log_errors,或编写自定义错误处理程序以缓冲错误。

另一个常见错误是文件开头的<?php标记之前或文件末尾的?>标记之前的偶然空格。