标头已发送错误 - cookies [重复]

This question already has an answer here:

<?php
$user = mysql_real_escape_string($_POST['login']);
$password = mysql_real_escape_string(sha1($_POST['password']));
$query = mysql_query("SELECT * FROM  `users` WHERE name =  '$user' AND pass =  '$password' AND privileges = 'superuser'");
$num_rows = mysql_num_rows($query);

if($num_rows == '1') {
$expire = time()*60*60*60*60;
setcookie("user","$user",$expire);
$_SESSION['user'] = $user;
include '/views/admin/admin.php';
}
else {
echo "Username and Password are incorrect! (Maybe you don't have permission!)";
}
?>

It says

Warning: Cannot modify header information - headers already sent by (output started at Z:\home\test1.ru\www\views\admin\validate.php:1) in Z:\home\test1.ru\www\views\admin\validate.php on line 9

line 9 is - setcookie("user","$user",$expire);

</div>

cookies in HTTP will be transferred using the headers. setcookie() therefore is just a wrapper around header() and cannot being used if there were already output in that script.

I guess that the output is an error message triggered by the mysql_* functions or whitespace|content before the opening <?php tag

i had similar problem.the reason for this is that you may have a white space before the php tag.Remove the space and you are good to go.

use ob_start(); after php tag

$user = mysql_real_escape_string($_POST['login']);

to

ob_start();

$user = mysql_real_escape_string($_POST['login']);

you have to check for few things.however i think your php script is ok.

  • 1.you have some html in this page before php begins. 2.you are using template for this site and template first sending header 3.you have some white space before php tag

check and be happy