【发布时间】:2011-05-27 08:55:47
【问题描述】:
我有一个奇怪的问题。我的页面顶部有一个 if 语句,当 header(location: xxx) commmand 在其中时,它似乎被忽略了。
$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
}
无论 $authorisation->check() 的结果如何,这总是重定向到 message.php 页面!
奇怪的是,当我注释掉 header-command,并将 echo 放入 if 语句中进行验证时,一切正常:
$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 时重定向到消息页面;
最后一个有趣的事情是我只在一台服务器上遇到了这个问题,同样的脚本在测试服务器上运行完美。
任何帮助将不胜感激!
【问题讨论】:
-
对不起..不清楚发生了什么..?
-
建议:在
header();之后也写exit(); -
添加了“exit;”重定向后(感谢您的建议),但这没有区别。
-
虽然听起来很愚蠢,但您是否在 failing 服务器上创建了 var_dump($check)?
-
按预期显示'bool(true)'
标签: php redirect header if-statement