[英]PHP Redirect - IF with multiple conditions
I'm having an issue with something quite simple and I have no idea why. 我有一个非常简单的问题,我也不知道为什么。
Need a fresh pair of eyes. 需要一双新鲜的眼睛。
In my configuration file I am pulling a users profile information, if their profile is incomplete and they are not in the: home, settings, logout, profile error page then they will be redirected. 在我的配置文件中,我正在提取用户个人资料信息,如果他们的个人资料不完整并且不在以下位置:主页,设置,注销,个人资料错误页面,则将对其进行重定向。
Basically I am making it mandatory or they won't be able to navigate to other areas of the system. 基本上,我是强制性的,否则他们将无法导航到系统的其他区域。
$link = $_SERVER["REQUEST_URI"];
if($counter<9 && ($link !="home" OR $link !="logout" OR $link !="profileError" OR $link !="profileSettings")){
header('Location: profileError');
kill();
}
I've tested my counter which seems to be working fine. 我已经测试了我的计数器,该计数器似乎工作正常。
Any help would be appreiated! 任何帮助将不胜感激!
$_SERVER['REQUEST_URI']
contains the full pathname of the file from the URL, eg /home.php
or /folder/logout.php
. $_SERVER['REQUEST_URI']
包含URL中文件的完整路径名,例如/home.php
或/folder/logout.php
。 So none of your $link
tests will work. 因此,您的
$link
测试都不起作用。 Try: 尝试:
$link = basename($_SERVER['REQUEST_URI'], '.php');
to get just the filename without the .php
extension. 以获得不带
.php
扩展名的.php
名。
Hi try to debug
your code first echo $link
and $counter
both or add else
to see whats wrong 嗨,请尝试先
debug
代码,然后同时echo $link
和$counter
两者,或添加else
以查看错误
$link = $_SERVER["REQUEST_URI"];
//echo $link; echo $counter;
if($counter<9 && ($link !="home" OR $link !="logout" OR $link !="profileError" OR $link !="profileSettings")){
header('Location: profileError');
kill();
}
else{
echo "there is some error"."</br>";
echo $link; echo $counter;
}
注释您的条件并测试标题位置,如果标题位置不起作用,请检查您的条件是否没有错误。
Managed to get it working. 设法使其工作。
$link = basename($_SERVER['REQUEST_URI'], '.php');
if($counter <9){
if($link !="home" OR $link !="logout" OR $link !="profileError" OR $link !="profileSettings"){
header('Location: profileError');
kill();
}
}
Not sure why splitting the IF statements helped but at least it works. 不知道为什么拆分IF语句会有所帮助,但至少可以奏效。
You should have used &&
condition operator instead of ||
您应该使用
&&
条件运算符而不是||
(OR). (要么)。
Translated : If the page is not called X and Y then ... Otherwise, If the page is not called X or Y then... it would always be called. 翻译:如果该页面未称为X和Y,则...否则,如果该页面未称为X或Y,则...它将始终被调用。
$link = basename($_SERVER['REQUEST_URI'], '.php');
if($counter <9){
if($link !="home" && $link !="logout" && $link !="profileError" && $link !="profileSettings"){
header('Location: profileError');
kill();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.