PHP 修复警告:无法修改标头信息 - 标头已发送

发布于 2024-10-16 11:58:58 字数 811 浏览 2 评论 0原文

警告:无法修改标头信息 - 已由 /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

她说她爱他 2024-10-23 11:58:58

您需要将所有 setcookie 函数调用移至输出上方,因为 setcookie 实际上发送 HTTP 标头,而在输出内容后您无法执行此操作。< /强>

You'll need to move all of your setcookie function calls above your output, because setcookie actually sends an HTTP header, which you cannot do after outputting content.

情话已封尘 2024-10-23 11:58:58

好吧,在开始输出后,您无法输出任何标头,包括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.

空气里的味道 2024-10-23 11:58:58

其实和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.

吹泡泡o 2024-10-23 11:58:58

如果您想在某人登录后设置 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.

鱼忆七猫命九 2024-10-23 11:58:58

确保设置 cookie 的调用是首先处理的事情。

所以这个:

  setcookie('username', "", time() - (60 * 60 * 24 * 365));
  setcookie('password', "", time() - (60 * 60 * 24 * 365));

应该在任何输出之前尽快调用。

要解决此问题,您可能需要做的就是删除:

并测试它。

然后使用以下命令将其重新添加到相关的 php 文件中:

echo '<div class="login">';

Ensure the call to set cookies is the first thing processed.

So this:

  setcookie('username', "", time() - (60 * 60 * 24 * 365));
  setcookie('password', "", time() - (60 * 60 * 24 * 365));

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:

echo '<div class="login">';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文