Php 检查referer是否为空或是否在数组中

Php 检查referer是否为空或是否在数组中,php,if-statement,symfony1,logic,Php,If Statement,Symfony1,Logic,我试图写一个if语句,基本上检查用户的推荐人是否在允许的推荐人列表中,如果没有失败 我有两个变量控制着这个$this->allowAllReferer和$this->allowEmptyReferer,它们根据名称分别决定是否允许每个引用者访问和是否允许空引用者。以及$this->allowedReferers,这是一个允许的referer数组 我有这个函数,我很确定它在下面不能正常工作,但我已经盯着它调整了半个小时,我已经到了无法判断它是否工作的地步 //If the referee is e

我试图写一个if语句,基本上检查用户的推荐人是否在允许的推荐人列表中,如果没有失败

我有两个变量控制着这个
$this->allowAllReferer
$this->allowEmptyReferer
,它们根据名称分别决定是否允许每个引用者访问和是否允许空引用者。以及
$this->allowedReferers
,这是一个允许的referer数组

我有这个函数,我很确定它在下面不能正常工作,但我已经盯着它调整了半个小时,我已经到了无法判断它是否工作的地步

//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
这个怎么样:

$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
中进行所有检查,可以使用
/
|
将条件链接在一起。短路评估确保变量值正确,并立即终止状态检查:

$ref = &$_SERVER['HTTP_REFERER'];
if($allowAll
    || ($allowEmpty && empty($ref))
    || (!empty($ref) && in_array($ref, $allowedReferers))) {
  // allowed
} else {
  // fail
}
这个怎么样:

$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
中进行所有检查,可以使用
/
|
将条件链接在一起。短路评估确保变量值正确,并立即终止状态检查:

$ref = &$_SERVER['HTTP_REFERER'];
if($allowAll
    || ($allowEmpty && empty($ref))
    || (!empty($ref) && in_array($ref, $allowedReferers))) {
  // allowed
} else {
  // fail
}

如果我正确理解了您的需求,那么这最符合您的原始代码

        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 } 

如果我正确理解了您的需求,那么这最符合您的原始代码

        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 } 

我会简化你的逻辑,比如:

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)
)。
其次,将逻辑检查分解为管理块,这样更易于编写、读取和维护。

我会简化您的逻辑,如下所示:

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)
)。
第二,将逻辑检查分解为管理块,这样更易于写入、读取和维护。

如果您希望将逻辑保持在一个巨大的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_数组为空,则第二次检查empty将跳过该数组。

如果要将逻辑保持在一个大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.");
}
if(isset($_SERVER['HTTP_REFERER'])) {
    echo $_SERVER['HTTP_REFERER'];
}

如果in_数组为空,则第二次检查empty将跳过该数组。

我更希望在一条语句中执行此操作,这是为了挑战,而不是为了感谢您的帖子。@pez:只需使用
|
而不是
else if
。短路魔法做REST如果它是空的但不允许是空的,这里会发生什么,第二行失败,所以第三行被检查为空值?我更愿意在一个语句中完成它,更多的是为了挑战,而不是感谢这篇文章。@pez:只要使用
|
而不是
else if
。短路魔法执行REST如果它是空的但不允许是空的,这里会发生什么,第二行失败,所以第三行用空值检查?您的真值表不完整;)它需要16(2^4)行^^ha!是的,我知道,我只是在四点之后失去了兴趣,以为那时我会把这个想法讲清楚的。你的真值表不完整;)它需要16(2^4)行^^ha!是的,我知道,只是在四点后失去了兴趣,我想我会在那时把这个想法讲清楚
if(isset($_SERVER['HTTP_REFERER'])) {
    echo $_SERVER['HTTP_REFERER'];
}