1
votes

Like I said in title. I have problem in CodeIgniter (version 3.1.6 and 3.1.7). In same method - it's working, but went I call userdata in other method, or other controller, or model I see only this:

print_r($this->session->userdata('user_session'));

Let's look at simple example (remember, it's not my full code!): So, controller Sessiontest.php:

class Sessiontest extends CI_Controller{

  public function __construct()
  {
    parent::__construct();
    //session library loaded by autoload.php
  }
  public function firstStep()
  {
    echo 'set userdata';
     $data = array(
      'example1' => '123',
      'example2' => 321
    );
    $this->session->set_userdata('user_session', $data);
    print_r($this->session->userdata('user_session'));
  }
  public function secondStep()
  {
    echo 'get userdata ';
    print_r($this->session->userdata('user_session'));
  }
}

And in browser: .../sessiontest/firststep:

set userdataArray ( [example1] => 123 [example2] => 321 )

.../sessiontest/secondstep:

get userdata

Same effect when I use files driver and database driver. Any help will be appreciated. I don't hide that I'm beginner, but I can't resolve this without any kind of help.

//Edit #1: Additional: Tested in Opera and in Chrome.

//Edit #2: Every refresh creates next new session file when I'm using files driver, and next new row in database, when I'm using database driver.

//Edit #3: Changes suggested in the answers.

1
Assign your session with a name and get with that name. eg:when assigning data to the session - $this->session->set_userdata('namehere', $data); and when getting session value $this->session->userdata('namehere');Ankit Singh
Are you destroying session somewhere in your code? Are you sure you're on the last version of CodeIgniter?cusmar
Yes, I downloaded it now, second time. @ankit-singh I put small edit, with same results.Matthew Mikos
Show us your session and cookie config settings.DFriend
then probably somewhere in your code your session is being destroy...Ankit Singh

1 Answers

-1
votes

$this->load->library('session'); //load seesion library

$this->session->set_userdata('name',$data); you have not give the name of session variable