警告:无法修改标题信息 - 已发送的标题 [英] Warning: Cannot modify header information - headers already sent

查看:1047
本文介绍了警告:无法修改标题信息 - 已发送的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

已通过PHP发送的标头


关于已经发送的标题当我成功登录页面。



这是我的代码处理登录:

  ?php 
include(config.php);
$ eUsername = $ _POST ['username'];
$ ePassword = $ _POST ['password'];

$ con = mysql_connect(localhost,MY_USERNAME,MY_PASSWORD);
if(!$ con)
{
die('Could not connect:'。mysql_error());
}

mysql_select_db(forum,$ con);
$ result = mysql_query(SELECT * FROM members WHERE username ='$ eUsername');

while($ row = mysql_fetch_array($ result))
{
if($ ePassword == $ row ['password']){
echo ;
setcookie(loggedIn,true,time()+ 1000000000);
setcookie(logUsername,$ eUsername,time()+ 100000000);
setcookie(logPassword,$ ePassword,time()+ 100000000);
}
else {
echo用户名/密码不正确,请重试。
}
}
mysql_close($ con);
if($ _COOKIE ['loggedIn'] ==true){
$ curURL = basename($ _ SERVER ['SCRIPT_NAME']);
echo您已经登录。< a href ='$ curURL?lo = true'>注销?< / a>;
}
echo< br />< br />;
print_r($ _ COOKIE);
?>

这样做基本上是如果你使用正确的信息登录,您的用户名,密码和一个检查其他两个。



但是当我成功登录时,会收到以下错误:


警告:无法修改头信息 - 已在第19行的/home/scott/web/forum/index.php中发送的头(输出在/home/scott/web/forum/index.php:18开始)



警告:无法修改/ home / scott / web / forum中的头信息(已在/home/scott/web/forum/index.php:18中输出) /index.php on line 20



警告:不能修改头信息 - 头文件已发送(输出开始于/home/scott/web/forum/index.php: 18)in /home/scott/web/forum/index.php on line 21


我做错了什么?

解决方案

您的 setcookie 调用之前可能会发生回声。



标头 setcookie 头文件必须在任何其他输出之前完成,否则会出现警告/错误。



此外,您应该检查config.php以确保没有尾随空格之后关闭?> php标记。记住... <?php ...?> 中包含的任何内容都被认为是由php解析器输出,并将获得echo'd / p>

Possible Duplicate:
Headers already sent by PHP

I'm getting errors about "headers already sent" when I successfully log into a page.

Here's my code that deals with the login:

<?php
include("config.php");
$eUsername = $_POST['username'];
$ePassword = $_POST['password'];

$con = mysql_connect("localhost","MY_USERNAME","MY_PASSWORD");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("forum", $con);
$result = mysql_query("SELECT * FROM members WHERE username = '$eUsername'");

while($row = mysql_fetch_array($result))
  {
    if ($ePassword==$row['password']) {
      echo "Correct";
       setcookie("loggedIn", "true", time()+1000000000);
       setcookie("logUsername", "$eUsername", time()+100000000);
       setcookie("logPassword", "$ePassword", time()+100000000);
    }
    else {
      echo "Incorrect username/password.  Please try again.";
    }
  }
mysql_close($con);
if ($_COOKIE['loggedIn']=="true") {
$curURL=basename($_SERVER['SCRIPT_NAME']);
echo "You are already logged in.  <a href='$curURL?lo=true'>Log out?</a>";
}
echo "<br /><br />";
print_r($_COOKIE);
?>

So basically what this does is if you log in with the correct information, it will set three cookies, your username, password and one to check for the other two.

But when I do log in successfully, I get these errors:

Warning: Cannot modify header information - headers already sent by (output started at /home/scott/web/forum/index.php:18) in /home/scott/web/forum/index.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /home/scott/web/forum/index.php:18) in /home/scott/web/forum/index.php on line 20

Warning: Cannot modify header information - headers already sent by (output started at /home/scott/web/forum/index.php:18) in /home/scott/web/forum/index.php on line 21

What am I doing wrong?

解决方案

You've got an echo which could occur before your setcookie call.

header or setcookie or anything else that sends HTTP headers has to be done before any other output, or you'll get that warning/error.

Also, you should check config.php to make sure there's no trailing whitespace after the closing ?> php tag. Remember... anything not inluded in <?php ... ?> is considered output by the php parser, and will get "echo'd" out.

这篇关于警告:无法修改标题信息 - 已发送的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆