【问题标题】:"headers already sent" error occurs ONLY when page opened with header() function仅当使用 header() 函数打开页面时才会出现“标头已发送”错误
【发布时间】:2017-01-03 14:25:24
【问题描述】:

所以我已经追踪这个“标头已发送”错误一周了。

我已经检查过了

  • 我的所有文件的顶部或底部都没有空格
  • 我没有 UTF_8 BOM 问题
  • 在调用 header() 之前没有打印、回声或其他输出

当我尝试直接从登录页面进入主页时发生错误:

  • 输入用户名和密码
  • 点击“登录”按钮
  • 这段代码执行:

    $redirect_to = "Location:" . BASE_URL . "src/home.php";
    exit(header($redirect_to));
    

我得到这个错误:

警告:无法修改标头信息 - 标头已由 /home/brekra50/groupxsolutions.com/index.php 中的 /home/brekra50/groupxsolutions.com/inc/header_start.html:10 发送(输出开始于 /home/brekra50/groupxsolutions.com/index.php) 103

但是....

如果我返回登录页面并单击页脚中的“联系我们”按钮,则“联系”页面会正常打开。如果我然后点击联系页面上的“主页”按钮,主页(刚刚发出警告)现在打开没有问题!

在这种情况下,我使用此代码通过单击按钮打开主页;

<li ><a href=<?php echo BASE_URL ."src/home.php"; ?> class=<?php echo set_nav_classes(HOME_PAGE, $curr_page); ?> >Home</a></li>

这显然是我的问题的一些线索,但我无法弄清楚。 header() 函数本身是否发送标头,以便从该函数打开主页时会触发警告?如果是这样,我该如何解决?

这是 header_start.html 第 10 行的代码:

<link rel="stylesheet" href= <?php echo BASE_URL . "css/fix.css"; ?> type="text/css" media="screen"> 

这是 index.php 文件中的代码,第 102 和 103 行:

$redirect_to = "Location:" . BASE_URL . "src/home.php";
exit(header($redirect_to));

“JustOn...”我认为已经为我指明了正确的方向。我在 index.php 页面的最顶部有两个“require_once()”调用,这必须在调用 header() 之前发生。我认为这是输出问题。

现在...该怎么办。

【问题讨论】:

  • output started at /home/brekra50/groupxsolutions.com/inc/header_start.html:10 是你的问题。
  • 你能把这个文件/home/brekra50/groupxsolutions.com/index.php on line 103的相关行贴出来吗?最好是第 103 行...
  • 你没有抓住重点,主页没有抛出那个错误,登录页面是处理表单的时候。
  • 你必须知道header()的使用和php创建headers和opputput的方式之间的联系:php.net/manual/de/function.header.php & php.net/manual/de/function.headers-sent.php & php.net/manual/de/ref.outcontrol.php
  • 您能否发布您从登录重定向到主页的代码部分以及主页的开头?

标签: php http-headers


【解决方案1】:

现在我知道“include()”和“require()”函数也是输出并导致我的错误,我不能只是摆脱它们,因为它们是整个网站设计的关键.

所以在 php 文档中我找到了一个代码 sn-p,它可以让我用 javascript 打开我的页面,从而避免了整个“标题启动”问题。

if (!headers_sent()) {
	$redirect_to = "Location:" . BASE_URL . "src/home.php";
	exit(header($redirect_to));
} else {
	$filename = BASE_URL . "src/home.php";
	echo '<script type="text/javascript">';
	echo 'window.location.href="' . $filename . '";';
	echo '</script>';

	echo '<noscript>';
	echo '<meta http-equiv="refresh" content="0;url=' . $filename . '" />';
	echo '<noscript>';

	exit;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 2016-11-13
    • 2015-12-03
    • 1970-01-01
    • 2020-11-02
    • 2011-08-31
    • 2013-09-30
    相关资源
    最近更新 更多