logout facebook connect session

2019-06-09 04:30发布

问题:

I'm trying to integrate my site with facebook connection to handle facebook signup and facebook login. i'm using codeigniter framework. But I got this problem now:

  1. current facebook login user is "test1". I go to my site to sign up with facebook, everything works fine and "test1"'s info are stored in my database. However, after I log test1 out from facebook, and login "test2" in on facebook, I go back to my site and do a signup with facebook again, it still "test1"'s info stored.

  2. I use ion auth library to handle user logout from my site. However, after I switch facebook test user accounts and do a "login with facebook" again, it still get the previous facebook user.

Based on the 2 cases above, it seems that the facebook session wasn't cleared up? I'm struggling on this problem for a long time, please help!

I user this to get user data: $fb_usr = $this->fb_connect->user; (it seems that no matter how the facebook user changs, fb_connect always return same user)

and fb_connect is sth like this:

    <?php
        include(APPPATH.'libraries/facebook/facebook.php');

        class Fb_connect extends Facebook{

            //declare public variables
            public  $user           = NULL;
            public  $user_id        = FALSE;

            public $fbLoginURL  = "";
            public $fbLogoutURL = "";

            public $fb          = FALSE;
            public $fbSession   = FALSE;
            public $appkey      = 0;

            //constructor method.
            public function __construct() 
            {
                        $CI = & get_instance();
                        $CI->config->load("facebook",TRUE);
                        $config = $CI->config->item('facebook');
                        parent::__construct($config);
                        $this->user_id = $this->getUser(); // New code
                        $me = null;
                        if ($this->user_id) {
                            try {
                                $me = $this->api('/me');
                                $this->user = $me;
                                } catch (FacebookApiException $e) {
                                    error_log($e);
                                }
                }   

                if ($me) {
                    $this->fbLogoutURL = $this->getLogoutUrl();
                } else {
                    $this->fbLoginURL = $this->getLoginUrl();
                }           
            } //end Fb_connect() function
}

回答1:

I think what you need to do is set the "next" param for your getLogoutUrl() call. Something like this:

$args['next'] = site_url('logout'); // replace "logout" with your controller which will clear the session
$fbLogoutURL = $facebook->getLogoutUrl($args);

Then, in the controller set as "next" you'll need to clear the session data.

class Logout extends CI_Controller {
    public function index() {
        $facebook->destroySession();       
        $this->session->sess_destroy();  // Assuming you have session helper loaded
        $this->load->view('logout');
    }
}

Let me know if that helps.