我正在尝试编写一个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
答案 0 :(得分:4)
如果您希望将逻辑保留在一个巨大的if块中,请尝试以下操作:
if (
// throw an error if it's empty and it's not allowed to be
(empty($_SERVER['HTTP_REFERER']) && !$this->allowEmptyReferer)
|| (
// don't bother throwing an error if all are allowed or empty is allowed
(!empty($_SERVER['HTTP_REFERER']) && !$this->allowAllReferer)
// throw an error if it's not in the array
&& !in_array((empty($_SERVER['HTTP_REFERER']) ? null : strtolower($_SERVER['HTTP_REFERER'])), $this->allowedReferers)
)
)
{
throw new sfException("Internal server error. Please contact system administrator. File download disabled.");
}
第二次检查为空,现在将跳过in_array,如果它是空的。
答案 1 :(得分:3)
这个怎么样:
$ref = &$_SERVER['HTTP_REFERER'];
if($allowAll) {
// allowed
} else if($allowEmpty && empty($ref)) {
// allowed
} else if(!empty($ref) && in_array($ref, $allowedReferers)) {
// allowed
} else {
// fail
}
如果您希望将所有支票都放在一个if
中,则只需使用or
/ ||
将条件链接在一起即可。短路评估可确保正确的变量值并立即终止条件检查:
$ref = &$_SERVER['HTTP_REFERER'];
if($allowAll
|| ($allowEmpty && empty($ref))
|| (!empty($ref) && in_array($ref, $allowedReferers))) {
// allowed
} else {
// fail
}
答案 2 :(得分:0)
如果我已正确理解您的要求,那么这与您的原始代码保持一致
if( (!$this->allowEmptyReferer && empty($_SERVER['HTTP_REFERER'])
|| (!$this->allowAllReferer && !in_array(
strtolower(empty($_SERVER['HTTP_REFERER']) ? null : $_SERVER['HTTP_REFERER']),
$this->allowedReferers)
) { // throw your exception }
答案 3 :(得分:0)
我会简化你的逻辑,就像这样:
if (!$this->allowAllReferer)
{
if (empty($_SERVER['HTTP_REFERER']) && !$this->allowEmptyReferer)
{
// emtpy referer - not allowed. handle as you wish (throw exception?)
}
else if (!empty($_SERVER['HTTP_REFERER']) &&
!in_array(strtolower($_SERVER['HTTP_REFERER'])), $this->allowedReferers)
{
// referer supplied is not approved/allowed. - handle appropriately.
}
else
{
// referer should be ok if we get here.
}
}
即。首先,如果你允许所有引用者,那么你不需要做任何处理 - 只需跳过这个(if (!this->allowAllReferer)
)。
其次,将逻辑检查分解为管理块,使其更易于编写,读取和维护。
答案 4 :(得分:0)
if(isset($_SERVER['HTTP_REFERER'])) {
echo $_SERVER['HTTP_REFERER'];
}