简体   繁体   English

PHP逻辑运算符组合

[英]PHP Logical Operator combination

I am using some PHP to redirect a website visitor if they are not logged in using the CMS's members addon. 如果没有使用CMS的成员插件登录网站访问者,我将使用一些PHP来重定向网站访问者。 I have this code which is working fine: 我有此代码,可以正常工作:

if (!perch_member_logged_in() && ($_SERVER["REQUEST_URI"] !== '/')){
  PerchSystem::redirect('/');
}

I now want to add a query string to see if someone has been redirected, and display an appropriate message. 现在,我想添加一个查询字符串以查看是否有人被重定向,并显示适当的消息。 I don't want to display this if someone comes directly to the home page. 如果有人直接进入主页,我不想显示此内容。 I tried this: 我尝试了这个:

if (!perch_member_logged_in() && (($_SERVER["REQUEST_URI"] !== '/') || ($_SERVER["REQUEST_URI"] !== '/?redirect=true'))){
  PerchSystem::redirect('/?redirect=true');
}

But I am getting too many redirects. 但是我收到太多重定向。 Can anyone help with this? 有人能帮忙吗?

I think you just need this: 我认为您只需要这样:

if (!perch_member_logged_in() && $_SERVER["REQUEST_URI"] !== '/?redirect=true') {
  PerchSystem::redirect('/?redirect=true');
}

If the user is not logged in and comes to your page, he can land on one of three pages: 如果用户未登录并进入您的页面,则他可以进入以下三个页面之一:

  • home page - do nothing 主页-不执行任何操作
  • home page with redirect=true set - also do nothing 设置了redirect=true主页-也不执行任何操作
  • any other page - redirect to homepage with redirect=true 任何其他页面-使用redirect=true重定向到主页

To start with I'd like to point you to the Operator Precedence page in the PHP manual. 首先,我想带您进入PHP手册中的“ 运算符优先级”页面 It explains in which order the operators are evaluated, which should help you get rid of some of the unnecessary parenthesis in your code. 它解释了运算符的评估顺序,这应该有助于您摆脱代码中的一些不必要的括号。
I suspect that this might solve your problem as well, depending upon what you mean with "too many redirects." 我怀疑这也可能解决您的问题,具体取决于您对“重定向过多”的含义。

Though, I suspect that you don't need to check for redirection upon root access as well. 不过,我怀疑您也不需要在根访问权限时检查重定向。 Hard to say for certain, as you haven't properly explained what your problem is, and what you expect to happen. 很难确定,因为您没有正确解释问题是什么,以及期望发生什么。
In other words, I (too) think your code should look like this: 换句话说,我(太)认为您的代码应如下所示:

if (!perch_member_logged_in() && $_SERVER["REQUEST_URI"] !== '/?redirect=true'){
    PerchSystem::redirect('/?redirect=true');
}

As that bit of code says: If the user is not logged in, and the redirect flag is set, redirect the user. 如该代码所示:如果用户未登录,并且设置了重定向标志,则重定向用户。

Another thing you should do, is to check the code in the PerchSystem::redirect() method, and make sure it uses die() after sending the Location header. 您应该做的另一件事是,检查PerchSystem::redirect()方法中的代码,并确保在发送Location标头后,它使用die() Otherwise your code will continue to execute, and cause unwanted behavior and/or security issues. 否则,您的代码将继续执行,并导致不必要的行为和/或安全问题。

Turns out I needed this: 原来我需要这个:

if (!perch_member_logged_in() && ($_SERVER["REQUEST_URI"] !== '/') && ($_SERVER["REQUEST_URI"] !== '/?redirect=true')){
  PerchSystem::redirect('/?redirect=true');
}

Thanks everyone who took the time to respond I appreciate it 感谢所有花时间回答的人,我非常感激

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM