getLogoutUrl没有使用php graphign api使用php codeigniter

时间:2015-05-30 18:08:58

标签: php facebook codeigniter facebook-graph-api facebook-php-sdk

当我点击我的注销网址时,我的facebook php sdk getLogoutUrl无效。 它会把我带回我给定的重定向网址,但它不会破坏我的facebook会话,我仍然可以在我的页面上看到我的var_dump($ fb_data)数组并注销网址。

这是我使用codeigniter的代码 我的lib_login库函数facebook代码

 public function facebook()
    {
        $facebook_default_scope = explode(',', $this->ci->config->item("facebook_default_scope"));
        $facebook_app_id = $this->ci->config->item("facebook_app_id");
        $facebook_api_secret = $this->ci->config->item("facebook_api_secret");

        // init app with app id and secret
        FacebookSession::setDefaultApplication($facebook_app_id, $facebook_api_secret);

        // login helper with redirect_uri
        $helper = new FacebookRedirectLoginHelper(site_url('login/facebook'));
        // see if a existing session exists
        if (isset($_SESSION) && isset($_SESSION['fb_token'])) {
            // create new session from saved access_token
            $session = new FacebookSession($_SESSION['fb_token']);

            // validate the access_token to make sure it's still valid
            try {
                if (!$session->validate()) {
                    $session = null;
                }
            } catch (Exception $e) {
                // catch any exceptions
                $session = null;
            }
        }

        if (!isset($session) || $session === null) {
            // no session exists

            try {
                $session = $helper->getSessionFromRedirect();
            } catch(FacebookRequestException $ex) {
                // When Facebook returns an error
                // handle this better in production code
                print_r($ex);
            } catch(Exception $ex) {
                // When validation fails or other local issues
                // handle this better in production code
                print_r($ex);
            }
        }

        // see if we have a session
        if (isset($session)) {
            // save the session
            $_SESSION['fb_token'] = $session->getToken();
            // create a session using saved token or the new one we generated at login
            $session = new FacebookSession($session->getToken());

            // graph api request for user data
            $request = new FacebookRequest($session, 'GET', '/me?fields=id,name,accounts{access_token,category,name,id,perms},permissions');
            $response = $request->execute();
            // get response
            $graphObject = $response->getGraphObject()->asArray();
            $logoutUrl = site_url('login');
            $fb_data = array(
                'me' => $graphObject,
                'loginUrl' => $helper->getLoginUrl($facebook_default_scope),
                'logoutUrl' => $helper->getLogoutUrl($session,$logoutUrl),
           );
            $this->ci->session->set_userdata('fb_data', $fb_data);

        } else {
            $fb_data = array(
                'me' => null,
                'loginUrl' => $helper->getLoginUrl($facebook_default_scope),
                'logoutUrl' => $helper->getLogoutUrl($session,$logoutUrl),
           );
            $this->ci->session->set_userdata('fb_data', $fb_data);
        }

        return $fb_data;
    }

这是我的控制器功能

 public function facebook()
    {
        $fb_data = $this->lib_login->facebook();


        if (isset($fb_data['me'])) {
            echo "<pre>";
             var_dump($fb_data);
            echo "</pre>";
            echo '<a href="' . $fb_data['logoutUrl'] . '">logout</a>';
        } else {
            echo '<a href="' . $fb_data['loginUrl'] . '">Login</a>';
        }
    }

当我使用此代码登录我的帐户时,我的页面上会显示注销网址和$ fb_data数组,但是当我注销并刷新我的网页时,它仍然是他们的。可以告诉我在这里做错了什么。< / p>

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library(array('session', 'lib_login'));
        $this->fb_data = $this->lib_login->facebook();
    }
    /**
     * facebook login
     *
     * @return void
     * @author appleboy
     **/
    public function facebook()
    {
        // check login data
        if (isset($this->fb_data['me'])) {
            var_dump($this->fb_data);
        } else {
            echo '<a href="' . $this->fb_data['loginUrl'] . '">Login</a>';
        }
    }

    public function logout()
    {
        if ( isset($this->fb_data['me']) ) {
            $this->session->unset_userdata('fb_data');
        }
        redirect('login/facebook', 'refresh');
    }

}
/* End of file login.php */
/* Location: ./application/controllers/login.php */