CodeIgniter Forums

Full Version: Facebook Connect - Up & Running w/ session issues - Elliots Library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]anndr0id[/eluser]
Hey! I'm hoping someone on here has used this library and has come up with a solution to this already.

I've got this set up and working great - http://www.haughin.com/code/facebook/

However I'm having issues with sessions. Even when I log the user out via facebook, the user variable is still holding on to info like name, pic, and email so when a site user logs out and logs back in as a different facebook user, its getting the wrong user information.

Theres a statement in the library:
Code:
elseif ( $user !== FALSE && $this->user_id === NULL )
    {
        // Need to destroy session
                
    }
which makes me assume he hasn't gotten around to coming up with a way to destroy the user session yet... especially if the user logs out on facebook and not the site itself.

Does anyone have any insight or clues on this?

Thanks!

El Forum

[eluser]anndr0id[/eluser]
Fixed it! For those of you looking in the future, I just changed the above code to:
Code:
elseif ($user !== FALSE && $this->user_id === NULL) {
    // Need to destroy session
    $this->_obj->session->unset_userdata('facebook_user');
}

and created a "logout" timeout in javascript to fix the facebook-rest-session error:
Code:
setTimeout("FB.Connect.logoutAndRedirect('index.php/logout/')", 300000);

and everything seems to be running smoothly. Smile

El Forum

[eluser]nealumney[/eluser]
I've been trying to locate a library (or something) which helps with CI. Where can I get 'Elliots Library' if that is what helps with facebook?

Is there any documentation on how it works?

Thanks,

Neal

El Forum

[eluser]CSeydel[/eluser]
Hi,
just want to submit a small fix for Elliots Facebook-Connect library, so that, if a user never logs out on my application but logs out in Facebook itself the session invalid error could be avoided:

Code:
<?php
/**
* CodeIgniter Facebook Connect Library (http://www.haughin.com/code/facebook/)
*
* Author: Elliot Haughin (http://www.haughin.com), [email protected]
*
* VERSION: 1.0 (2009-05-18)
* LICENSE: GNU GENERAL PUBLIC LICENSE - Version 2, June 1991
*
**/


    include(APPPATH.'libraries/facebook-client/facebook.php');

    class Facebook_connect {

        private $_obj;
        private $_api_key        = NULL;
        private $_secret_key    = NULL;
        public     $user             = NULL;
        public     $user_id         = FALSE;
        
        public $fb;
        public $client;

        function Facebook_connect()
        {
            $this->_obj =& get_instance();

            $this->_obj->load->config('facebook');
            $this->_obj->load->library('session');
            
            $this->_api_key        = $this->_obj->config->item('facebook_api_key');
            $this->_secret_key    = $this->_obj->config->item('facebook_secret_key');

            $this->fb = new Facebook($this->_api_key, $this->_secret_key);
            
            $this->client = $this->fb->api_client;
            
            if($this->fb->get_loggedin_user()){
                try
                {
                    $uid             = $this->fb->api_client->users_getInfo($this->fb->get_loggedin_user(), 'uid');
                    $this->user_id  = $uid[0]['uid'];
                }
                catch (Exception $ex)
                {
                    $this->fb->clear_cookie_state();
                }
            }

            $this->_manage_session();
        }

        private function _manage_session()
        {
            $user = $this->_obj->session->userdata('facebook_user');
            
            if ( $user === FALSE && $this->user_id !== NULL )
            {
                $profile_data = array('uid','first_name', 'last_name', 'name', 'locale', 'pic_square', 'profile_url');
                $info = $this->fb->api_client->users_getInfo($this->user_id, $profile_data);

                $user = $info[0];

                $this->_obj->session->set_userdata('facebook_user', $user);
            }
            elseif ( $user !== FALSE && $this->user_id === NULL )
            {
                // Need to destroy session
                $this->_obj->session->unset_userdata('facebook_user');    
            }

            if ( $user !== FALSE )
            {
                $this->user = $user;
            }
        }
    }

El Forum

[eluser]JanDoToDo[/eluser]
With this addition I get an error that clear cookie state is not a valid method in Facebook ?!?

El Forum

[eluser]CSeydel[/eluser]
I am using facebook.php and facebookapi_php5_restlib.php that can be found here:
http://wiki.developers.facebook.com/index.php/PHP
or for direct download:
http://pearhub.org/get/facebook-0.1.0.tgz

El Forum

[eluser]JanDoToDo[/eluser]
Cheers. Got that. I had to change the conditions in manage_session to :

$this->user_id !== FALSE instead of $this->user_id !== NULL

everytime it occurred as it is set to "FALSE" by default, which doesnt equate to "NULL" and so it never actually triggered the false condition. Has this not happened to you?

El Forum

[eluser]CSeydel[/eluser]
after several tries, the error never occured at my application.
possible states at my app are:

1. user never logged it, app shows connect button, user connects an generates a new session
2. user returns to app with valide facebook session, app notice it via get_loggedin_user()
3. user returns to app with invalide facebook session, app kills old cookie via clear_cookie_state() and the connect button appears again

El Forum

[eluser]JanDoToDo[/eluser]
I have another problem -

If the user is logged into facebook and goes through the connect procedure its fine

However, if the user is not logged into facebook, when the click the fb connect icon they get a "enter your username & password" but when they submit then, it processes the login (as they are now logged into fb) but the popup box then refreshes and displays "page cannot be found"

Any ideas?

As I said, if they are already logged into facebook it works fine, but if they are not currently logged in it doesnt work!