警告:没办法修改标头信息 - 标头已发送
问题说明
可能的重复:
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='https://www.swvq.com/$curURL?lo=true'>Log out?</a>";
}
echo "<br /><br />";
print_r($_COOKIE);
?>
所以基本上它的作用是如果您使用正确的信息登录,它会设置三个 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:
警告:无法修改标题信息 - 标题已由(输出开始于/home/scott/web/forum/index.php:18)在/home/scott/web/forum/index.php 中第 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 19
警告:无法修改标题信息 - 标题已由(输出开始于/home/scott/web/forum/index.php:18)在/home/scott/web/forum/index.php 第 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 20
警告:无法修改标题信息 - 标题已由(输出开始于/home/scott/web/forum/index.php:18)在第 21 行的/home/scott/web/forum/index.php 中发送
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
我做错了什么?
正确答案
您有一个回声,它可能在您的 setcookie
调用之前发生.
You've got an echo which could occur before your setcookie
call.
header
或 setcookie
或任何其他发送 HTTP 标头的内容必须在任何其他输出之前完成,否则您将收到警告/错误.
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.
此外,您应该检查 config.php 以确保在关闭 ?>
php 标记之后没有尾随空格.请记住...任何未包含在 <?php ... ?>
中的内容都被 php 解析器视为输出,并且会被回显"出来.
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.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanghijbg
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20