eskimo
eskimo

Reputation: 2624

session codeigniter localhost

I'm using this Facebook lib for CodeIgniter so users can login with Facebook. Works perfectly fine on production, but not on localhost. The thing that is bugging me is that it used to work fine on localhost, but suddenly stopped working.

I've been searching for some time and I'm expecting that this has something to do with CI's session library, but I'm not 100% sure.

Background info

The problem

What I already tried and did not work

Additional info on config.php

$config['cookie_prefix']    = "";
$config['cookie_domain']    = "";
$config['cookie_path']      = "/";
$config['cookie_secure']    = FALSE;

I hoped that using PHP's native session would resolve the problem, but it didn't. Not sure what else I can try.

For reference the full Facebook.php library file code

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( session_status() == PHP_SESSION_NONE ) {
    session_start();
}

require_once( APPPATH . 'libraries/facebook/Facebook/GraphObject.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/GraphSessionInfo.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookSession.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookCurl.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookHttpable.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookCurlHttpClient.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookResponse.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookSDKException.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookRequestException.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookAuthorizationException.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookRequest.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookRedirectLoginHelper.php' );

use Facebook\GraphSessionInfo;
use Facebook\FacebookSession;
use Facebook\FacebookCurl;
use Facebook\FacebookHttpable;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookResponse;
use Facebook\FacebookAuthorizationException;
use Facebook\FacebookRequestException;
use Facebook\FacebookRequest;
use Facebook\FacebookSDKException;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\GraphObject;

class Facebook {
    var $ci;
    var $helper;
    var $session;

    public function __construct() {
        $this->ci =& get_instance();

        FacebookSession::setDefaultApplication( $this->ci->config->item('api_id', 'facebook'), $this->ci->config->item('app_secret', 'facebook') );
        $this->helper = new FacebookRedirectLoginHelper( $this->ci->config->item('redirect_url', 'facebook') );

        if ( $this->ci->session->userdata('fb_token') ) {
            $this->session = new FacebookSession( $this->ci->session->userdata('fb_token') );

            // Validate the access_token to make sure it's still valid
            try {
                if ( ! $this->session->validate() ) {
                  $this->session = false;
                }
            } catch ( Exception $e ) {
                // Catch any exceptions
                $this->session = false;
            }
        } else {
            try {
                $this->session = $this->helper->getSessionFromRedirect();
            } catch(FacebookRequestException $ex) {
                // When Facebook returns an error
            } catch(\Exception $ex) {
                // When validation fails or other local issues
            }
        }

        if ( $this->session ) {
            $this->ci->session->set_userdata( 'fb_token', $this->session->getToken() );

            $this->session = new FacebookSession( $this->session->getToken() );
        }
    }


/*
|--------------------------------------------------------------------------
| Login URL
|--------------------------------------------------------------------------
*/

    public function get_login_url() {
        return $this->helper->getLoginUrl( $this->ci->config->item('permissions', 'facebook') );
    }


/*
|--------------------------------------------------------------------------
| Logout URL
|--------------------------------------------------------------------------
*/

    public function get_logout_url() {
        if ( $this->session ) {
            return $this->helper->getLogoutUrl( $this->session, site_url() );
        }
        return false;
    }


/*
|--------------------------------------------------------------------------
| Get user data
|--------------------------------------------------------------------------
*/

    public function get_user() {
        if ( $this->session ) {
            try {
                $user = (new FacebookRequest( $this->session, 'GET', '/me' ))->execute()->getGraphObject()->asArray();

                return $user;

            } catch(FacebookRequestException $e) {
                return false;

            }
        }
    }
}

Upvotes: 16

Views: 2354

Answers (2)

Ben Broadley
Ben Broadley

Reputation: 632

The library you are using is external to the core CodeIgniter codebase and therefore isn't able to communicate with native functions in the normal way; instead of using $this as your reference to the CodeIgniter instance, in your library you should be using $this->ci so to access session data, your code would look something like:

$this->ci->session->userdata();

This is explained in more detail in the CodeIgniter documentation here

Upvotes: 3

Muhammad El-Saeed
Muhammad El-Saeed

Reputation: 62

in the view use $this->facebook->session instead of $this->session and it will work ;)

Upvotes: 0

Related Questions