【问题标题】:Checking if the referer is empty or if it is in an array检查引用者是否为空或是否在数组中
【发布时间】:2011-08-25 11:21:50
【问题描述】:

我正在尝试编写一个 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

【问题讨论】:

  • 你的真值表不完整 ;) 它需要 16 (2^4) 行 ^^
  • 哈!是的,我知道,只是在四点后失去了兴趣,并认为到那时我会明白这个想法的 xD

标签: php if-statement symfony1 logic


【解决方案1】:

如果您想将逻辑保留在一个巨大的 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 为空,第二次检查现在将跳过 in_array。

【讨论】:

    【解决方案2】:

    这个怎么样:

    $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
    }
    

    【讨论】:

    • 我更愿意在一份声明中做到这一点,更多的是为了挑战而不是感谢您的帖子。
    • @pez:只需使用|| 而不是else if。剩下的就是短路魔法
    • 如果它为空但不允许为空,会发生什么情况,第二行失败,所以第三行检查为空值?
    【解决方案3】:

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

            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 } 
    

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:
        if(isset($_SERVER['HTTP_REFERER'])) {
            echo $_SERVER['HTTP_REFERER'];
        }
        

        【讨论】:

          猜你喜欢
          • 2011-11-04
          • 2013-02-24
          • 2012-07-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-13
          相关资源
          最近更新 更多