简体   繁体   中英

Clear session and cookie on logout from Facebook using the Facebook PHP SDK

I am using the Facebook Codeigniter PHP SDK. This is how I get the login user from facebook API:

public $loginuser = "";
function __construct(){
    parent::__construct();
    /* .......
       .......
    */
    $fb_config = array(
        'appId' => $this->config->item('appID'),
        'secret' => $this->config->item('appSecret'),
        'default_graph_version' => $this->config->item('graphVersion')
    );
    $fb = $this->load->library('facebook', $fb_config);
    $this->loginuser = $this->facebook->getUser();
}

And to check whether the user logged in from Facebook or not:

function index($requestuser, $requestid){
    if ($this->loginuser) {
        /*do something here */
    }else{
        $myurl = site_url()."/Crowd/index/".$requestuser."/".$requestid;
        redirect($this->facebook->getLoginUrl(array(
            'scope' => 'email,user_likes,user_friends',
            'redirect_uri' => $myurl
        )));
    }
}

There are two problems that I am encountering:

  1. When logging out from Facebook (not from the application), the session and cookies on the application are not destroyed, so the user is considered as logged in even though the user has logged out from Facebook.

  2. When logging out from the application, the session is not destroyed either.

Here is the logout url:

$logout_url = $this->facebook->getLogoutUrl(array('next' => site_url() . '/user/logout'));

And here is what is done inside the User/logout controller:

public function logout(){
    $this->session->unset_userdata('id');
    $this->session->unset_userdata('email');
    $this->session->unset_userdata('firstname');
    $this->session->unset_userdata('lastname');
    $this->session->unset_userdata('status');
    $this->session->unset_userdata('isLoggedIn');
    $this->session->sess_destroy();
    session_destroy();

    // Redirect to baseurl
    redirect(base_url());
    //redirect('login/index');
}

How can I check the logged user ? And how to destroy the Facebook login session upon logging out ?

Check if the user still logged in on his/her facebook account

if ($facebook->getUser())
{
    try
    {
        $user = $facebook->api('/me');
        //continue
    }
    catch(FacebookApiException $e){
        $facebook->destroySession();
        //destroy
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM