Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • 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'] to FALSE instead of TRUE
  • 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;

            }
        }
    }
}
like image 520
eskimo Avatar asked Apr 12 '15 21:04

eskimo


People also ask

How to use session in CodeIgniter?

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.

How to start session in CodeIgniter 4?

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.


1 Answers

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

like image 69
Ben Broadley Avatar answered Sep 22 '22 04:09

Ben Broadley