生产中使用facebook sdk的cakephp 2.x中的会话问题

生产中使用facebook sdk的cakephp 2.x中的会话问题,cakephp,facebook-php-sdk,Cakephp,Facebook Php Sdk,我正在尝试使用FacebookPHPSDK和CakePHP2.x进行登录。 它正在使用调试模式1或2,但不使用调试模式0。 似乎会话在生产中无法正常工作。 我在网上搜索了很多次,但没有找到适合我的解决方案 我详细阅读了这两个线程,但没有解决这个问题。 我在AppController中使用这两个函数进行登录 public function beforeFilter() { $this->disableCache(); $this->Facebook = new Fa

我正在尝试使用FacebookPHPSDK和CakePHP2.x进行登录。 它正在使用调试模式1或2,但不使用调试模式0。 似乎会话在生产中无法正常工作。 我在网上搜索了很多次,但没有找到适合我的解决方案

我详细阅读了这两个线程,但没有解决这个问题。

我在AppController中使用这两个函数进行登录

public function beforeFilter()
{
    $this->disableCache();

    $this->Facebook = new Facebook(array(
        'app_id'                => 'appId',
        'app_secret'            => 'appSecret',
        'default_graph_version' => 'v2.7',
    ));

    $this->Auth->allow(['.....']);
}

public function login()
{
    if (!session_id()) {
        session_start();
    }
    $this->loadModel("User");

    $user_id = $this->Session->read('Auth.User.id');

    $fb          = $this->Facebook->getRedirectLoginHelper();
    $permissions = ['email']; // Optional permissions

    $callback_url = HTTP_ROOT . 'login';
    $fb_login_url = $fb->getLoginUrl($callback_url, $permissions);

    $this->set('fb_login_url', $fb_login_url);

    if (!empty($user_id)) {
        //redirect to profile page if already logged in
        $this->redirect(... . );
    }

    //local login request
    if ($this->request->is('post')) {
        ......
    }

    // when facebook login is used
    elseif ($this->request->query('code')) {
        try {
            $accessToken = $fb->getAccessToken();

        } catch (\Facebook\Exceptions\FacebookResponseException $e) {
            // When Graph returns an error
            $this->Session->setFlash('Graph returned an error: ' . $e->getMessage(), 'error');
            $this->redirect($this->referer());
        } catch (\Facebook\Exceptions\FacebookSDKException $e) {
            // When validation fails or other local issues
            $this->Session->setFlash('Facebook SDK returned an error: ' . $e->getMessage(), 'error');
            $this->redirect($this->referer());
        }

        if (!isset($accessToken)) {
            if ($fb->getError()) {
                header('HTTP/1.0 401 Unauthorized');
                $this->Session->setFlash("Error: " . $fb->getError() . "\n", 'error');
                $this->Session->setFlash("Error Code: " . $fb->getErrorCode() . "\n", 'error');
                $this->Session->setFlash("Error Reason: " . $fb->getErrorReason() . "\n", 'error');
                $this->Session->setFlash("Error Description: " . $fb->getErrorDescription() . "\n", 'error');
                $this->redirect($this->referer());
            } else {
                header('HTTP/1.0 400 Bad Request');
                $this->Session->setFlash('Bad request', 'error');
                $this->redirect($this->referer());
            }
        }

        // Logged in
        $oAuth2Client = $this->Facebook->getOAuth2Client();

        $tokenMetadata = $oAuth2Client->debugToken($accessToken);
        $tokenMetadata->validateAppId('1200125790051089'); // Replace {app-id} with your app id
        $tokenMetadata->validateExpiration();

        if (!$accessToken->isLongLived()) {
            try {
                $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
            } catch (\Facebook\Exceptions\FacebookSDKException $e) {
                $this->Session->setFlash('Error getting long-lived access token: ' . $helper->getMessage() . "</p>\n\n", 'error');
                $this->redirect($this->referer());
            }
        }

        $_SESSION['fb_access_token'] = (string) $accessToken;
        $fb_access_token             = (string) $accessToken;

        if (isset($accessToken)) {
            try {
                // Returns a `Facebook\FacebookResponse` object
                $response = $this->Facebook->get('/me?fields=id,first_name,last_name,email', $accessToken);
            } catch (\Facebook\Exceptions\FacebookResponseException $e) {
                $this->Session->setFlash('Graph returned an error: ' . $e->getMessage(), 'error');
                $this->redirect($this->referer());
            } catch (\Facebook\Exceptions\FacebookSDKException $e) {
                $this->Session->setFlash('Facebook SDK returned an error: ' . $e->getMessage(), 'error');
                $this->redirect($this->referer());
            }

            $fb_user = $response->getGraphUser();

            // We will varify if a local user exists first
            $local_user = $this->User->find('first', array(
                'conditions' => array('facebook_id' => $fb_user['id']),
            ));

            // If exists, we will log them in
            if ($local_user) {
                $this->Auth->login($local_user['User']);
            } else {
                // we will create new user with facebook_id and log them in
                $data['User'] = array(.........);

                // You should change this part to include data validation
                $new_user = $this->User->save($data);
                $this->Auth->login($new_user['User']);
            }
            // redirect to profile page here
        }
    }
}
公共函数beforeFilter()
{
$this->disableCache();
$this->Facebook=新Facebook(数组)(
'应用程序id'=>'应用程序id',
“app_secret”=>“appSecret”,
“默认图形版本”=>“v2.7”,
));
$this->Auth->allow(['…']);
}
公共函数登录()
{
如果(!session_id()){
会话_start();
}
$this->loadModel(“用户”);
$user_id=$this->Session->read('Auth.user.id');
$fb=$this->Facebook->getRedirectLoginHelper();
$permissions=['email'];//可选权限
$callback_url=HTTP_ROOT.'login';
$fb\u login\u url=$fb->getLoginUrl($callback\u url,$permissions);
$this->set($fb\u login\u url',$fb\u login\u url);
如果(!empty($user_id)){
//如果已登录,则重定向到配置文件页面
$this->redirect(..);
}
//本地登录请求
如果($this->request->is('post')){
......
}
//使用facebook登录时
elseif($this->request->query('code')){
试一试{
$accessToken=$fb->getAccessToken();
}捕获(\Facebook\Exceptions\FacebookResponseException$e){
//当图形返回错误时
$this->Session->setFlash('Graph返回错误:'。$e->getMessage(),'error');
$this->redirect($this->referer());
}catch(\Facebook\Exceptions\FacebookSDKException$e){
//验证失败或其他本地问题时
$this->Session->setFlash('Facebook SDK返回错误:'。$e->getMessage(),'error');
$this->redirect($this->referer());
}
如果(!isset($accessToken)){
如果($fb->getError()){
标题(“HTTP/1.0 401未经授权”);
$this->Session->setFlash(“错误:”..fb->getError()。“\n”,“错误”);
$this->Session->setFlash(“错误代码:”..fb->getErrorCode()。“\n”,“Error”);
$this->Session->setFlash(“错误原因:”.$fb->getErrorReason().“\n”,“错误”);
$this->Session->setFlash(“错误描述:”.$fb->getErrorDescription().“\n”,“错误”);
$this->redirect($this->referer());
}否则{
标头(“HTTP/1.0 400错误请求”);
$this->Session->setFlash('Bad request','error');
$this->redirect($this->referer());
}
}
//登录
$oAuth2Client=$this->Facebook->getOAuth2Client();
$tokenMetadata=$oAuth2Client->debugToken($accessToken);
$tokenMetadata->validateAppId('1200125790051089');//用应用程序id替换{app id}
$tokenMetadata->validateExpiration();
如果(!$accessToken->isLongLived()){
试一试{
$accessToken=$oAuth2Client->getLongLivedAccessToken($accessToken);
}catch(\Facebook\Exceptions\FacebookSDKException$e){
$this->Session->setFlash('Error get long-live access token:'。$helper->getMessage()。“

\n\n','Error'); $this->redirect($this->referer()); } } $\会话['fb\ U访问\ U令牌']=(字符串)$accessToken; $fb_访问_令牌=(字符串)$accessToken; if(isset($accessToken)){ 试一试{ //返回“Facebook\FacebookResponse”对象 $response=$this->Facebook->get('/me?fields=id,名字,姓氏,email',$accessToken); }捕获(\Facebook\Exceptions\FacebookResponseException$e){ $this->Session->setFlash('Graph返回错误:'。$e->getMessage(),'error'); $this->redirect($this->referer()); }catch(\Facebook\Exceptions\FacebookSDKException$e){ $this->Session->setFlash('Facebook SDK返回错误:'。$e->getMessage(),'error'); $this->redirect($this->referer()); } $fb_user=$response->getGraphUser(); //如果首先存在本地用户,我们将进行更改 $local_user=$this->user->find('first',array( 'conditions'=>array('facebook\u id'=>$fb\u user['id']), )); //如果存在,我们将登录它们 if($本地用户){ $this->Auth->login($local_user['user']); }否则{ //我们将创建具有facebook_id的新用户并登录 $data['User']=数组(……); //您应该更改此部分以包括数据验证 $new_user=$this->user->save($data); $this->Auth->login($new_user['user']); } //重定向到此处的配置文件页面 } } }
我对SDK和CakePHP 2.x也有一些问题。我编写了一个小处理程序,让SDK使用CakeSession

你可以在这里找到它:


您所说的会话不能正常工作到底是什么意思?另外,您不需要在登录时手动启动会话,cake应该自动处理它。