为什么getSessionFromRedirect()返回NULL? [英] Why is getSessionFromRedirect() return a NULL?

查看:97
本文介绍了为什么getSessionFromRedirect()返回NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,该代码将用户重定向到Facebook,并尝试检索会话,但该会话为NULL:

I have the following code which redirects the user to log into facebook and tries to retrieve the session but the session is NULL:

<?php

session_start();

require 'vendor/autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;

FacebookSession::setDefaultApplication('Foo', 'Bar');

$helper = new FacebookRedirectLoginHelper('Baz');
$loginUrl = $helper->getLoginUrl();  

echo '<a href="' . $loginUrl . '">Log In</a>';  

$session = $helper->getSessionFromRedirect();  

// This displays [NULL] always
echo '[' . gettype($session) . ']';

?>

我不明白为什么$ session总是为NULL.请帮忙.

I don't understand why the $session is always NULL. Please help.

推荐答案

它的发生是由于getLoginUrl()函数

It happens because of the getLoginUrl() function,

因为当我们提到与FB重定向页面相同的页面时,它一次又一次调用getLoginUrl()函数.这会更改$this->state变量值

because when we mentioned same page as FB redirect page, it call getLoginUrl() function again and again.. which change $this->state variable value

,然后isValidRedirect()getSessionFromRedirect()始终返回null

and then isValidRedirect() and getSessionFromRedirect() always return null

以下是使用与FB重定向页面相同的页面的解决方案:

Here is solution to use same page as FB redirection page:

在Facebook将您重定向到同一页面时,请勿调用getLoginUrl(),只需添加适当的会话检查即可检查有效的Facebook会话(请参考以下代码示例)

Don't call getLoginUrl() when facebook redirect you to same page, just add the proper session check to check valid facebook session (Please refer following code example)

session_start();
require("facebook/autoload.php");


use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;

FacebookSession::setDefaultApplication('appid84', '0secret0a626c6');

$helper = new FacebookRedirectLoginHelper('http://tmd.local/fblogin.php');

try {
  $session = $helper->getSessionFromRedirect();
  // var_dump($session);
} catch(FacebookRequestException $ex) {
} catch(\Exception $ex) {
}
if ($session) {
  var_dump($session);
}
else
{
  $loginUrl = $helper->getLoginUrl();
  header("location:".$loginUrl);
  exit;
}

这篇关于为什么getSessionFromRedirect()返回NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆