Priska Aprilia
Priska Aprilia

Reputation: 1149

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 ?

Upvotes: 0

Views: 1379

Answers (1)

forbidden
forbidden

Reputation: 19

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
    }
}

Upvotes: 1

Related Questions