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
- I'm running MAMP with PHP 5.5.10 and Chrome 41.0.2272.118
- Locally I've configured the httpd.conf in MAMP so my local url is a 'real' URL like www.example-local.net and Apache port is 80
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 working- The production and development code are exactly the same, except for the Facebook API key and secret since I use a test app locally. As indicated; it worked perfectly fine before and I haven't changed my test app settings.
- EDIT: I have my TLD configured as app domain and site URL in the FB test app. Again, this doesn't seem to be the issue since
The 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)- It also seems that therefore the library doesn't set
fb_boken
in the session userdata and that's why I suspect there's something wrong with setting$this->session
- I did move to a different timezone (just 1 hour) and I read somewhere that this could cause issues? EDIT; I've been back in the timezone where it worked previously and it's still not working
What I already tried and did not work
- In
config.php
setting$config['sess_expiration']
to a high value (eg 999999) instead of 7200 - In
config.php
changing$config['cookie_domain']
to the localhost website (http://www.example-local.net
, tried with and without trailing slash) instead of empty ("") - In
config.php
changing$config['sess_match_useragent']
toFALSE
instead ofTRUE
- Replace app/system/library/Session.php with PHP's native session as described here
- Clear all my cookies and browsing data
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;
}
}
}
}