AlexW.H.B.
AlexW.H.B.

Reputation: 1871

Why am I losing my session on page changes?

So I'm trying to integrate the PHP facebook SDK/API with codeigniter, because I love codeigniter, but for some reason I keep losing my facebook session as soon as I change pages from my base URL, I have printed the $_session and all i get is Array ( [fb_339393762762289_state] => cb8c201ed66ebcaa60407114aa92f001 ) any ideas why this is happening?

Here is what my controller looks like:

class Main extends CI_Controller {

var $user;
var $log_in_url;
var $log_out_url;

function __construct() {
    parent::__construct();
    $this->load->library('facebook');
    $perams = array(
        'scope' => 'user_about_me, read_friendlists',
        'redirect_uri' => 'http://www.example.com/'
    );
    $this->user = $this->facebook->getUser();
    $this->log_out_url = $this->facebook->getLogoutUrl();
    $this->log_in_url = $this->facebook->getLoginUrl($perams);

    print_r($this->user);
    print_r($this->log_out_url);
    print_r($this->log_in_url);
}

public function index() {
    if ($this->user) {
        try {
            $data['user_profile'] = $this->facebook->api('/me');
        } catch (FacebookApiException $e) {
            error_log($e);
            $this->user = null;
        }
    }

    if ($this->user) {
        $data['logout_url'] = $this->log_out_url;
    } else {
        $data['login_url'] = $this->log_in_url;
    }
    $data['user'] = $this->user;
    $this->load->view('templet/header', $data);
    $this->load->view('main view');
    $this->load->view('templet/footer');
}

  public function account() {

  if ($this->user) {
  try {
  $data['user_profile'] = $this->facebook->api('/me');
  } catch (FacebookApiException $e) {
  error_log($e);
  $this->user = null;
  }
  }

    if ($this->user) {
        $data['logout_url'] = $this->log_out_url;
    } else {
        $data['login_url'] = $this->log_in_url;
    }
  $data['user'] = $this->user;
  $this->load->view('templet/header', $data);
  $this->load->view('account');
  $this->load->view('templet/footer');
  }

from what I can understand from the source code of the api is that it is in charge of handling sessions, so I don't understand why it keeps losing it.

I was thinking it could maybe have something to do with my Htaccess set up.. Ie. my url when i change pages looks like this www.example.com/index.php/controller_name/method_name/

also do i need to do anything with the $_GET vars? because that is one thing i noticed.. there seem to be some things passed in the URL, but they don't continue on to other pages.

any help on this would be greatly appreciated. I am really at a loss.

Upvotes: 2

Views: 2094

Answers (2)

AlexW.H.B.
AlexW.H.B.

Reputation: 1871

my problem was somewhat simple, I kept loosing my session data any time I changed pages, and it tuns out the solution was quite simple. in the config file of CI you have to put you websites base URL, and I put and the session was being created for http://www.mysite.com/ so the php sdk/api created one session for the first url and another for the second.. thus i lost all of the data as soon as i changed pages. so it's a simple problem, but took a lot of trouble shooting.

Upvotes: 0

matheuzzy
matheuzzy

Reputation: 472

If you use CodeIgniter Session Library you must know that "The Session class does not utilize native PHP sessions. It generates its own session data"

CodeIgniter Session Class

Upvotes: 1

Related Questions