当header(Location:xxx)在里面时,PHP if-statement被忽略 [英] PHP if-statement ignored when header(Location: xxx) is inside
问题描述
$ check = $ authorisation-> check();
//我知道这个结果是真实的,通过回显价值
//(你必须相信我这个)
if(!$ check){
//重定向到消息
header(Location:message.php);
出口;
} else {
//什么都不做,继续页面
}
这总是重定向到message.php页面,不管$ authorisation-> check()的结果是什么!
奇怪的是,当我注释掉header-command,并且在if语句中加上echo作为验证时,所有的工作都是可以预料的:
$ check = $ authorisation-> check(); // true $ b $ if if(!$ check){
//重定向到消息
echo你不受欢迎;
} else {
echo你可以输入;
}
结果是您可以输入;
这也符合预期:
$ check = true;
if(!$ check){
//重定向到消息
header(Location:message.php);
出口;
} else {
//什么也不做
}
当$ check = false时,它只会重定向到消息页面;
最后一个有趣的事情是我仅在1台服务器上遇到问题,同样的脚本完美地运行testserver。
任何帮助都将非常感谢!
你在完成标题后,应始终运行 exit
,以便传输速度更快,更稳定。
试试这个方法:
$ $ $ $ $ $ $ $ $ $ $ .PHP);
出口;
}
// ...
请阅读其他提示的评论为什么这是一个好主意。
I have a strange problem. There is an if-statement at the top of my page that seems to be ignored when a header(location: xxx) commmand is inside of it.
$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
}
This ALWAYS redirects to the message.php page, no matter what the outcome of $authorisation->check() is!
Strange thing is, when I comment out the header-command, and put echo's in the if-statement for verification, all works as to be expected:
$check = $authorisation->check(); // true
if(!$check){
// redirect to message
echo "you are not welcome here";
}else{
echo "you may enter";
}
The result is "you may enter";
This also works as expected:
$check = true;
if(!$check){
// redirect to message
header("Location: message.php");
exit;
}else{
// do nothing
}
This only redirects to the message page when $check = false;
Last funny thing is that I experience the problem only on 1 server, same script works flawlessly on testserver.
Any help would be much appreciated!
you should always run exit
after you are finished with headers so that the transfer is faster and more stable for the browser.
Try this way:
if( ... )
{
header("Location: message.php");
exit;
}
// ...
Please read the comment for other tip's on why this is a good idea.
这篇关于当header(Location:xxx)在里面时,PHP if-statement被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!