检查引用者是否为空或者是否在数组中

发布于 2024-12-01 06:23:21 字数 1472 浏览 1 评论 0原文

我正在尝试编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小梨窩很甜 2024-12-08 06:23:21

如果您想将逻辑保留在一个巨大的 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。

If you would like to keep the logic within one huge if block, then try the following:

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.");
}

The second check for empty will now skip the in_array if it's empty.

毁梦 2024-12-08 06:23:21

怎么样:

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

How about this:

$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 you want to have all checks in a single if, you can simply chain together the conditions using or/||. Short-circuit evaluation ensures proper variable values and immediate termination of the condition check:

$ref = &$_SERVER['HTTP_REFERER'];
if($allowAll
    || ($allowEmpty && empty($ref))
    || (!empty($ref) && in_array($ref, $allowedReferers))) {
  // allowed
} else {
  // fail
}
抚笙 2024-12-08 06:23:21

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

        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 I've understood your requirements correctly, then this is most in keeping with your original code

        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 } 
风蛊 2024-12-08 06:23:21

我会简化你的逻辑,像这样:

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

I would simplify your logic, something like this:

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

}

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.

棒棒糖 2024-12-08 06:23:21
if(isset($_SERVER['HTTP_REFERER'])) {
    echo $_SERVER['HTTP_REFERER'];
}
if(isset($_SERVER['HTTP_REFERER'])) {
    echo $_SERVER['HTTP_REFERER'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文