This question already has an answer here:
这个问题在这里已有答案:
- How to fix “Headers already sent” error in PHP 11 answers
- 如何修复PHP 11答案中的“Headers already sent”错误
Here's my file. I want to make it redirect, but nothing happens. To check out what is going on, I added an echo before the header part.
这是我的档案。我想让它重定向,但没有任何反应。为了查看发生了什么,我在标题部分之前添加了一个echo。
It neither throws an error or redirect to index.php. What is wrong? I have turned output buffering on/off, but nothing makes it redirect. What can I do?
它既不会抛出错误,也不会重定向到index.php。哪里不对?我打开/关闭输出缓冲,但没有任何东西使它重定向。我能做什么?
<?
error_reporting(E_ALL);
echo 'This is an error';
header("Location: login.php");
die();
?>
Thanks
谢谢
4 个解决方案
#1
44
From PHP documentation :
从PHP文档:
header()
must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
必须在通过普通HTML标记,文件中的空行或PHP发送任何实际输出之前调用header()。
And in your case, you are using echo
before header()
在你的情况下,你在header()之前使用echo
#2
1
I reminded myself that I had xDebug installed on the actual test environment and after googling it, I found this site: http://bugs.xdebug.org/view.php?id=532
我提醒自己,我在实际的测试环境中安装了xDebug,在谷歌搜索后,我找到了这个网站:http://bugs.xdebug.org/view.php?id = 532
So I'll downloaded the last version of xDebug and changed the php.ini accordingly for the new file and everything works out like a charm. Headers are being sent - the redirecetion is done and errors are displayed.
因此,我将下载最新版本的xDebug,并相应地更改了php.ini以获取新文件,所有内容都像魅力一样。正在发送标头 - 重定向已完成并显示错误。
Thanks everybody for your help!
谢谢大家的帮助!
#3
1
Do you have short tags enabled? try it with the long tag <?php
:
你有短标签吗?尝试使用长标签<?php:
<?php
error_reporting(E_ALL);
header("Location: login.php");
die();
?>
#4
0
Is display_errors enabled?
@Gumbo - It sure is!是否启用了display_errors? @Gumbo - 肯定是!
well, it sure is not. because
好吧,肯定不是。因为
To check out what is going on, I added an echo before the header part.
为了查看发生了什么,我在标题部分之前添加了一个echo。
if you'd have display_errors enabled, it would display an error, at least when you turned output buffering on
.
如果您启用了display_errors,它将显示错误,至少在您打开输出缓冲时。
So, first of all make sure you can see error messages.
just print out an undefined variable,
因此,首先要确保您可以看到错误消息。只打印出一个未定义的变量,
error_reporting(E_ALL);
echo $undef9306;
I am sure you won't see it. So, you have to turn displaying errors on
我相信你不会看到它。因此,您必须打开显示错误
Next, on the server side you can use headers_sent()
function to see if headers were sent. On client side use HTTP sniffer to see if anything were sent
接下来,在服务器端,您可以使用headers_sent()函数来查看是否已发送标头。在客户端使用HTTP嗅探器查看是否发送了任何内容
And check your file for the BOM.
并检查文件中的BOM。
#1
44
From PHP documentation :
从PHP文档:
header()
must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
必须在通过普通HTML标记,文件中的空行或PHP发送任何实际输出之前调用header()。
And in your case, you are using echo
before header()
在你的情况下,你在header()之前使用echo
#2
1
I reminded myself that I had xDebug installed on the actual test environment and after googling it, I found this site: http://bugs.xdebug.org/view.php?id=532
我提醒自己,我在实际的测试环境中安装了xDebug,在谷歌搜索后,我找到了这个网站:http://bugs.xdebug.org/view.php?id = 532
So I'll downloaded the last version of xDebug and changed the php.ini accordingly for the new file and everything works out like a charm. Headers are being sent - the redirecetion is done and errors are displayed.
因此,我将下载最新版本的xDebug,并相应地更改了php.ini以获取新文件,所有内容都像魅力一样。正在发送标头 - 重定向已完成并显示错误。
Thanks everybody for your help!
谢谢大家的帮助!
#3
1
Do you have short tags enabled? try it with the long tag <?php
:
你有短标签吗?尝试使用长标签<?php:
<?php
error_reporting(E_ALL);
header("Location: login.php");
die();
?>
#4
0
Is display_errors enabled?
@Gumbo - It sure is!是否启用了display_errors? @Gumbo - 肯定是!
well, it sure is not. because
好吧,肯定不是。因为
To check out what is going on, I added an echo before the header part.
为了查看发生了什么,我在标题部分之前添加了一个echo。
if you'd have display_errors enabled, it would display an error, at least when you turned output buffering on
.
如果您启用了display_errors,它将显示错误,至少在您打开输出缓冲时。
So, first of all make sure you can see error messages.
just print out an undefined variable,
因此,首先要确保您可以看到错误消息。只打印出一个未定义的变量,
error_reporting(E_ALL);
echo $undef9306;
I am sure you won't see it. So, you have to turn displaying errors on
我相信你不会看到它。因此,您必须打开显示错误
Next, on the server side you can use headers_sent()
function to see if headers were sent. On client side use HTTP sniffer to see if anything were sent
接下来,在服务器端,您可以使用headers_sent()函数来查看是否已发送标头。在客户端使用HTTP嗅探器查看是否发送了任何内容
And check your file for the BOM.
并检查文件中的BOM。