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
if($this->session)
in a view returns true. EDIT; I don't actually use this in a view but it's just to test that this is workingThe problem
if($this->session)
in the (Facebook) library file returns false. EDIT; this seems to be the issue. So this is working in the view, not in the library. The weird thing about it is that this was working before (no code change)fb_boken
in the session userdata and that's why I suspect there's something wrong with setting $this->session
What I already tried and did not work
config.php
setting $config['sess_expiration']
to a high value (eg 999999) instead of 7200config.php
changing $config['cookie_domain']
to the localhost website (http://www.example-local.net
, tried with and without trailing slash) instead of empty ("")config.php
changing $config['sess_match_useragent']
to FALSE
instead of TRUE
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;
}
}
}
}
Sessions data are available globally through the site but to use those data we first need to initialize the session. We can do that by executing the following line in constructor. $this->load->library('session'); After loading the session library, you can simply use the session object as shown below.
Initializing a Session php $session = \Config\Services::session($config); The $config parameter is optional - your application configuration. If not provided, the services register will instantiate your default one. Alternatively, you can use the helper function that will use the default configuration options.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With