检查引用者是否为空或者是否在数组中
我正在尝试编写一个 if 语句,该语句基本上检查用户引荐来源网址是否在允许的引荐来源网址列表中,如果不是则失败。
我有两个变量控制这个 $this->allowAllReferer
和 $this->allowEmptyReferer
,根据它们的名称决定是否每个引用者都应该被允许访问以及是否为空分别允许推荐人。以及 $this->allowedReferers
,它是允许的引用者数组。
我有这个功能,我很确定它不能正常工作,但我已经盯着它调整了半个小时,我已经到了我无法判断它是否正常工作的地步。
//If the referee is empty and allow empty referrer is false
//or
//If it is not in the allowed list and allow all referer is false
if(!(empty($_SERVER['HTTP_REFERER']) && $this->allowEmptyReferer)
&&
!(!$this->allowAllReferer && in_array(
strtolower(empty($_SERVER['HTTP_REFERER']) ? null : $_SERVER['HTTP_REFERER']), //Silly php access null variable
$this->allowedReferers)
)) {
throw new sfException("Internal server error. Please contact system administrator. File download disabled.");
}
您知道正确的或更好的方法吗/您能确认上述工作吗?
案例,希望这能让大家更清楚
empty_referrer | allowEmpty | in_array | allReferer | result
----------------------------------------------------------------
true | true | false | false | false - no error - empty allowed
false | true | false | false | true - error - not in array
false | true | false | true | false - no error - not in array but allowed
false | false | false | false | true - error - empty and now allowed
I am trying to write an if statement that basically checks if the users referrer is in a list of allowed referrers and if not fails.
I have two variables controlling this $this->allowAllReferer
and $this->allowEmptyReferer
, which as per their name decide whether every referrer should be allowed access and whether empty referrers are allowed, respectively. As well as $this->allowedReferers
which is an array of allowed referrers.
I have this function below which I am pretty sure isn't working properly but I have been staring at and tweaking it for half an hour and i've got to the point where I can't tell if it's working or not.
//If the referee is empty and allow empty referrer is false
//or
//If it is not in the allowed list and allow all referer is false
if(!(empty($_SERVER['HTTP_REFERER']) && $this->allowEmptyReferer)
&&
!(!$this->allowAllReferer && in_array(
strtolower(empty($_SERVER['HTTP_REFERER']) ? null : $_SERVER['HTTP_REFERER']), //Silly php access null variable
$this->allowedReferers)
)) {
throw new sfException("Internal server error. Please contact system administrator. File download disabled.");
}
Do you know the correct or a better way to do this/can you confirm the above works?
Cases, hope this makes it more clear
empty_referrer | allowEmpty | in_array | allReferer | result
----------------------------------------------------------------
true | true | false | false | false - no error - empty allowed
false | true | false | false | true - error - not in array
false | true | false | true | false - no error - not in array but allowed
false | false | false | false | true - error - empty and now allowed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想将逻辑保留在一个巨大的 if 块中,请尝试以下操作:
如果 in_array 为空,则第二次检查现在将跳过 in_array。
If you would like to keep the logic within one huge if block, then try the following:
The second check for empty will now skip the in_array if it's empty.
怎么样:
如果您想在一个
if
中进行所有检查,您可以简单地使用or
/||
将条件链接在一起。短路评估可确保正确的变量值并立即终止条件检查:How about this:
If you want to have all checks in a single
if
, you can simply chain together the conditions usingor
/||
. Short-circuit evaluation ensures proper variable values and immediate termination of the condition check:如果我正确理解了您的要求,那么这最符合您的原始代码
If I've understood your requirements correctly, then this is most in keeping with your original code
我会简化你的逻辑,像这样:
即。首先,如果您允许所有引用者,那么您不需要进行任何处理 - 只需跳过此操作(
if (!this->allowAllReferer)
)。其次,将逻辑检查分解为管理块,这使得编写、读取和维护变得更容易。
I would simplify your logic, something like this:
ie. First of all if you are allowing all referers, then you don't need to do any processing - just skip this (
if (!this->allowAllReferer)
).Second, break up your logic checks into management chunks it makes it easier to write, read and maintain.