PHP 修复警告:无法修改标头信息 - 标头已发送
警告:无法修改标头信息 - 已由 /home/blocexco/public_html/homepage.php:73 中的 /home/blocexco/public_html/classes/mysql/mysql.security.php 第 99 行发送的标头
此错误在第 100 行对 mysql.security.php 重复第二次。
homepage:73
<div class="login">
<?php require_once 'login.php'; ?>
</div>
mysql.security.php: 99-100
setcookie('username', "", time() - (60 * 60 * 24 * 365));
setcookie('password', "", time() - (60 * 60 * 24 * 365));
我知道这不是我读过的“BOM”问题。在调用 header() 和 setcookie() 函数之前和之后都有输出 - 这是必要的,因为主页包含一个 php 文件,然后该文件会注入正确的登录或注销表单。
我听说过在内容开头使用 ob_start() ,但这不是一个非常具体的指令...我尝试将它放在 homepage.php 的开头(就在 html 标签之前),但这并没有解决任何问题。
我是 PHP 新手(几天后,总体来说是网络应用程序开发新手)。说实话,让我大吃一惊的是,我不能仅仅通过 php 更改我所在的页面或创建 cookie,而无需向后弯腰......
Warning: Cannot modify header information - headers already sent by (output started at /home/blocexco/public_html/homepage.php:73) in /home/blocexco/public_html/classes/mysql/mysql.security.php on line 99
This error is repeated a second time for mysql.security.php on line 100.
homepage:73
<div class="login">
<?php require_once 'login.php'; ?>
</div>
mysql.security.php: 99-100
setcookie('username', "", time() - (60 * 60 * 24 * 365));
setcookie('password', "", time() - (60 * 60 * 24 * 365));
I know this isn't a "BOM" issue as I've read about. There is output before and after my calls to header() and setcookie() functions - this is necessary since the homepage includes a php file which then injects the right login or logout form.
I've heard about using ob_start() at the beginning of content, but that's not a very specific instruction...I tried placing it at the beginning of homepage.php (just before the html tag) and that didn't fix anything.
I'm new to PHP (a few days in, and new to web-app dev in general). To be honest, it blows my mind that I can't just change which page I am on or create cookies, via php without bending over backwards...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要将所有
setcookie
函数调用移至输出上方,因为setcookie
实际上发送 HTTP 标头,而在输出内容后您无法执行此操作。< /强>You'll need to move all of your
setcookie
function calls above your output, becausesetcookie
actually sends an HTTP header, which you cannot do after outputting content.好吧,在开始输出后,您无法输出任何标头,包括cookie。期间。
这是一个简单的技术限制,因为 HTTP 标头需要在 HTTP 主体之前发送。这不是 PHP 的问题。
您应该以首先决定应发送什么内容并收集所有必要数据的方式构建应用程序,然后继续输出实际的 HTML。无论你怎么看,在 HTML 主体内混合逻辑都是混乱的。例如,查看 MVC 模式。
Well, you can't output any headers, including cookies, after output was started. Period.
It's a simple technical restriction, since HTTP headers need to be sent before the HTTP body. It's not PHP that's at fault here.
You should structure your app in a way that first decides what should be sent and gathers all the necessary data, then proceeds to output the actual HTML. Mixing logic inside the main HTML body is messy however you look at it. Look into the MVC pattern for example.
其实和php没有任何关系。基本原则是您的浏览器首先需要标题,然后才是内容。
现在您只发送了一次标头。因此,如果您执行的操作主要包括发送标头(例如
header()
),那么显然应该首先执行此操作。如果您已经发送了标头(隐式发送,因为您开始输出),则无法再次发送它们。
底线是,如果你想控制你的标题,你必须做第一件事。这就像演出开始后想要更改介绍一样。如果您使用ob_start函数,您会将所有输出保存到某个点。我不会推荐这个;让您的程序流畅,以便您知道要发送哪些标头,并首先发送它们。然后调用产生输出的函数。
It doesn't really have anything to do with php. The basics is that your browser expects headers first, content later.
Now you sent headers only once. So if you do an action that consist mainly of sending headers (like
header()
) it should be obvious that this should be done first.If you already sent headers (implicitly, because you started output), you can't send them again.
Bottomline is that if you want to control your headers, you have to do that first thing. It's like wanting to change the intro after the show has allready started. If you use the
ob_start
functions you are saving all output to a cerain point. I would not recommend this; Make your program flow so you know what headers you want to sent, and send them first. Then call the functions that produce output.如果您想在某人登录后设置 cookie,那么您可以在发布表单的另一个 .php 文档中设置 cookie,以验证用户的输入。不过,他们仍然必须超越任何产出。
If you are wanting to set the cookies after someone has logged in then you could set the cookies in another .php document that the form is posted to authenticate the user's inputs. They would still have to go above any output though.
确保设置 cookie 的调用是首先处理的事情。
所以这个:
应该在任何输出之前尽快调用。
要解决此问题,您可能需要做的就是删除:
并测试它。
然后使用以下命令将其重新添加到相关的 php 文件中:
Ensure the call to set cookies is the first thing processed.
So this:
should be called as soon as possible, before any output.
To fix this all you may need to do is remove:
and test it.
Then re-add it in the relevant php file using: