PHP header() 不起作用
有人知道为什么我的 header() 不重定向吗?
我的脚本的最后一部分是:
header("location: test.php");
die('died');
它写道:
died.
:(((
它应该在死之前重定向,但它没有。
你有什么想法吗?
Does somebody know why my header() does not redirect?
The last part of my script is:
header("location: test.php");
die('died');
It writes:
died.
:(((
It should has to redirect before it dies, but it does not.
Do you have you any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可能在向浏览器输出一些文本/HTML 之后调用 header(),这是禁忌。即使在调用之前发送了一个输出空间,您的 header() 调用也将被忽略。
换句话说,在向用户显示任何内容之前,您的 header() 代码需要位于脚本的开头。如果仍然不起作用,请确保您的 php 标记之外没有任何空格或其他空白。
It's probably that you're calling header() after you are outputting some text/HTML to the browser, which is a no-no. Your header() call will be ignored if even so much as a single space of output has been sent before the call.
In other words, your header() code needs to be at the start of your script, before you display anything to the user. If that still isn't working, make sure you don't have any spaces or other whitespace by mistake outside of your php tags.
您应该为位置标头使用完全限定的地址,并且不输出任何内容:
此外,请确保在设置标头之前没有发送其他内容,否则将不会发送,因为标头将具有已经发送到浏览器了。
You should use a fully-qualified address for your location header, and not output any content:
Additionally make sure that no other content was sent before setting the header, otherwise it wont be sent as the headers will have already been sent to the browser.
也许在
header
之前有一些不可见的输出,这会阻止设置header
,并且信息警告会被抑制。另外,位置标头应包含绝对 URL:
Maybe there is some invisible output before
header
, which is preventing settingheader
, and informative warnings are suppressed.Additionally Location-Headers should contain an absolute URL:
位置:
应为位置:
。此外...
祝你好运。
location:
should beLocation:
.Moreover...
Good luck.
只需删除 php 标记之前的所有内容即可解决此问题。
删除 php 标记之前的空格
选择标记之前的所有内容,然后按擦除按钮。
Just resolve it by removing ALL things before the php tag.
Remove the SPACE before the php tag
Select all before the tag and then push the erase button.
我也被这个错误困扰,并尝试使用 error_reporting(E_ALL) 显示错误,
但这对我不起作用。信息:我在 Strato 上使用托管服务。
Floem 回答了所有这些问题的解决方案,感谢这个出色的设置。
如果您输入
error_reporting(E_ALL)
一次后没有收到错误,-> 您必须另外添加
ini_set('display_errors', 'On')
,以强制 PHP 显示所有错误。这解决了我的错误问题,然后可以读出为什么 PHP 不会进行重定向。
我这边的一个提示:我建议您构建一个小重定向类,如以下视频所示: 重定向类以方便使用,如果您要使用此重定向超过通常。
I was stuck on this error, too, and tried to show errors with error_reporting(E_ALL),
but this didn't worked for me. Info: I was using a hosting service on Strato.
The solution to all this was answered by Floem, thanks for this great setup.
If you ever don't get errors in case you put
error_reporting(E_ALL)
once in,-> You must additionally add
ini_set('display_errors', 'On')
, to force PHP to show all your Errors. This solved my Error issue onThen it's possible to read out, why PHP wouldn't make your redirect.
One tip on my side: I would recommend you to build a little redirect Class, as shown in this Video: Redirect Class for easy use, if you're going to use this redirect more than usual.
如果 HTML 文件中直接/间接需要包含 'header()' 指令的文件,则指令 {include 'someFile'} 应该是该 HTML 文件的第一行。将此说明放在 HTML 文件的顶部,如此图片所示。
If the file containing the 'header()' instruction is required directly/indirectly in a HTML file, then the instruction {include 'someFile'} should be the first line of that HTML file. Place this instruction on the top of your HTML file, as shown in this image.
添加
ob_start();
之前
Add
ob_start();
before
在最初的问题中,die() 函数在之前有一个 header() 的情况下被执行。
我遇到了类似的问题,所以我创建了下面的代码来测试。
然后...?那么,执行
header()
函数之后以及只有 header() 返回到index.php
页面之后出现的代码。所有这些都表明
header()
函数可以在脚本末尾使用来引用另一个脚本。但是要小心如果它位于脚本的开头,它不会阻止脚本运行。
In the original question the die() function is executed while there is a header() just before.
I had a similar problem so I created the code below to test.
And...? Well the code present after the
header()
function is executed and after only the header() returns to theindex.php
page.All that to say that the
header()
function can be used at the end of a script to refer to another script.But BE CAREFUL if this is positioned at the start of the script, it does not prevent the script from running.