我有一个奇怪的问题。在我的页面顶部有一个if语句,当标题(位置:xxx)命令在其中时,它似乎被忽略了。
$check = $authorisation->check();
// i know this results in true by echoing the value
// (you've got to believe me on this one)
if(!$check){
// redirect to message
header("Location: message.php");
exit;
}else{
// do nothing, continue with page
}
无论$authorization->check()的结果如何,它总是重定向到message.php页面!
奇怪的是,当我注释掉头命令,并在if语句中放入echo进行验证时,所有操作都如预期:
$check = $authorisation->check(); // true
if(!$check){
// redirect to message
echo "you are not welcome here";
}else{
echo "you may enter";
}
结果是"您可以输入";
这也如预期的那样起作用:
$check = true;
if(!$check){
// redirect to message
header("Location: message.php");
exit;
}else{
// do nothing
}
这只会在$check=false时重定向到消息页面;
最后一件有趣的事情是,我只在一台服务器上遇到了这个问题,同样的脚本在测试服务器上完美地工作。
任何帮助都将不胜感激!
重定向到另一个页面后,调用exit函数,否则将执行以下代码。
if(!$check){
// redirect to message
header("Location: message.php");
exit;
}else{
// do nothing, continue with page
}
// the following code will be executed if exit is not called
...
您应该始终在处理完头之后运行exit
,这样浏览器的传输速度更快、更稳定。
试试这个方法:
if( ... )
{
header("Location: message.php");
exit;
}
// ...
请阅读评论,了解为什么这是个好主意。
试着放error_reporting(-1);
,你会看到一些新的东西。在您的一台服务器上,PHP错误报告设置为较低级别。
这类错误通常是由在调用头函数之前将内容发送到浏览器引起的。
即使你不认为你在发送内容,如果你的文件在"<?php"之前以空格或空行开头,那么你也会遇到错误——这通常是一件很微妙的事情。
输出缓冲可以允许您在"发送"内容之后调用头函数——这可能就是页面在一台服务器上工作而不是在另一台服务器的原因。
建议:
-
在顶部写入
ob_start()
和 -
也在
header();
之后写入exit();
-
按照一个答案中的建议使用
error_reporting(-1)
进行调试
我也遇到了同样的问题,您正在代码中的其他地方调用header('Location:bullah')。检查一下。