【问题标题】:How can I solve the warning "Warning: array_key_exists."?如何解决警告“警告:array_key_exists。”?
【发布时间】:2013-04-29 06:33:18
【问题描述】:

我正在使用 Hybridauth 社交登录,当用户使用 Facebook 进行身份验证时,我收到以下错误:

警告:array_key_exists() [function.array-key-exists]:第二个 参数应该是数组或对象 /hybridauth/Hybrid/thirdparty/Facebook/base_facebook.php 在第 1328 行

我对为什么会发生这种情况的猜测(可能是错误的)是因为用于传递给 Hybridauth 的参数来自浏览器 URL,而且我有两个 - page=register 和 connected_with=facebook。 Hybridauth 只需要第二个...

它实际上进行了身份验证,但我想摆脱这个错误。为什么会出现此警告?有办法隐藏吗?

这是出错的地方:

 /**
   * Get the base domain used for the cookie.
   */
  protected function getBaseDomain() {
    // The base domain is stored in the metadata cookie  
    // if not we fallback to the current hostname
    $metadata = $this->getMetadataCookie();
    if (array_key_exists('base_domain', $metadata) &&
        !empty($metadata['base_domain'])) {
      return trim($metadata['base_domain'], '.');
    }
    return $this->getHttpHost();
  }

警告来自此代码:

  /**
   * Destroy the current session
   */
  public function destroySession() {
    $this->accessToken = null;
    $this->signedRequest = null;
    $this->user = null;
    $this->clearAllPersistentData();

    // JavaScript sets a cookie that will be used in getSignedRequest 
    // that we need to clear if we can
    $cookie_name = $this->getSignedRequestCookieName();
    if (array_key_exists($cookie_name, $_COOKIE)) {
      unset($_COOKIE[$cookie_name]);
      if (!headers_sent()) {
        $base_domain = $this->getBaseDomain();
        setcookie($cookie_name, '', 1, '/', '.'.$base_domain);
      } else {
        // @codeCoverageIgnoreStart
        self::errorLog(
          'There exists a cookie that we wanted to clear that we couldn\'t '.
          'clear because headers was already sent. Make sure to do the first '.
          'API call before outputting anything.'
        );
        // @codeCoverageIgnoreEnd
      }
    }
  }

【问题讨论】:

    标签: session authentication hybridauth array-key array-key-exists


    【解决方案1】:

    看起来getMetadataCookie() 并不总是返回一个数组,可能是因为尚未设置 cookie。在使用它之前,您可能需要检查它是否真的是一个数组;

    if (is_array($metadata) && array_key_exists('base_domain', $metadata) &&
    

    对于添加的代码,同样适用于新代码中的array_key_exists()。如果在未设置 cookie 的情况下不确定它是否实际设置为数组,请先检查。

    【讨论】:

    • 感谢您的帮助,我不完全理解,但是您知道为什么它仍然会进行身份验证吗?这个警告对我来说是个问题吗?
    • 谢谢,你给我指出了正确的方向。我只是把整个 if 子句拿出来:“if (array_key_exists($cookie_name, $_COOKIE))”,因为它还说我们需要“如果可以的话”清除 cookie..也可以继续清除它..警告已经消失......也许不是最好的解决方案,但警告已经消失
    猜你喜欢
    • 2012-06-14
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多