Facebook登录不符合CSRF状态令牌的问题

Joh*_*ohn 5 .htaccess facebook facebook-php-sdk

我做了一些搜索,但没有发现任何与我的问题有关的内容.

我目前正在尝试在我的网站上实现Facebook登录,并且由于htaccess mod重写URL,我遇到登录验证问题?

代码工作得很好,如果我使用它没有mod重写规则,我会登录,如:

domain.com/view_webhosting.php?webhosting=name
Run Code Online (Sandbox Code Playgroud)

但是一旦我转到mod重写URL

domain.com/webhosting-name/
Run Code Online (Sandbox Code Playgroud)

然后它只是不起作用并抛出错误"CSRF状态令牌与提供的不匹配".

在htaccess文件中它看起来像这样

RewriteRule ^webhosting-([a-z_0-9-]+)/$ /view_webhosting.php?webhosting=$1 [L]
Run Code Online (Sandbox Code Playgroud)

任何人都有这样的问题的解决方案?我正在使用Facebook SDK v3.1.1

bis*_*ark 7

PHP SDK期望在重定向之后'state'字段位于$ _REQUEST(我相信作为GET参数),然后才能为访问令牌交换'code'.来自base_facebook.php:

protected function getCode() {
  if (isset($_REQUEST['code'])) {
    if ($this->state !== null &&
      isset($_REQUEST['state']) &&
      $this->state === $_REQUEST['state']) {

      // CSRF state has done its job, so clear it
      $this->state = null;
      $this->clearPersistentData('state');
      return $_REQUEST['code'];
    } else {
      self::errorLog('CSRF state token does not match one provided.');
      return false;
    }
  }

  return false;
}
Run Code Online (Sandbox Code Playgroud)

你的RewriteRule可能会踩到那个参数.


Joh*_*ohn 7

谢谢bismark.

你是对的; 它无法获得GET参数,解决方案如下:

RewriteRule ^webhosting-([a-z_0-9-]+)/$ /view_webhosting.php?webhosting=$1 [L]
Run Code Online (Sandbox Code Playgroud)

RewriteRule ^webhosting-([a-z_0-9-]+)/$ /view_webhosting.php?webhosting=$1 [QSA,L]
Run Code Online (Sandbox Code Playgroud)

查询字符串追加[QSA]

•'qsappend|QSA' (query string append)
This flag forces the rewrite engine to append a query string part of the substitution string
to  the existing string, instead of replacing it. Use this when you want to add more data
to the query string via a rewrite rule.
Run Code Online (Sandbox Code Playgroud)

谢谢你们,让我走上正轨!