Reputation: 141
Experts,
i have a little Problem with my sessions. I want to save my login data into the session like this:
checklogin Controller
$user = $this->user_model->user($email, $password);
$user["logged_in"] = TRUE;
var_dump($this->session->set_userdata($user)); // return NULL? is this correct?
var_dump($this->session->all_userdata()); //return the correct data
Now the Session is saved up to a redirect.
Other Controller
var_dump($this->session->all_userdata()); // return session_id,… and a empty user_data array
I think I have the same Problem with the Shopping Cart Class.
Can anyone help?
Upvotes: 2
Views: 10392
Reputation: 911
I got a similar issue when I want to rerun an old website that was built formerly using CI 3.0.6 on a server running PHP 7.3.
I have set a session in one controller.
$this->session->set_userdata('logged_in', TRUE);
Then, the other controllers that are accessed after that cannot retrieve the information while a new record in the session table in the database is stored.
empty($_SESSION['logged_in']); // true
empty($this->session->logged_in); // true
The solution that works for me is as follows.
id
column in the CI session table. For example, our session table name is ci_sessions
.ALTER TABLE ci_sessions CHANGE id id varchar(128) NOT NULL;
system
directory with newer version. In my case, it works with the system
directory from CI 3.1.10.In summary, a newer CI version is required to be able to interact well with newer PHP (7.1+). It has a longer id
value for the stored session record. Rather than downgrading your PHP version, it is better to upgrade the CI version.
Upvotes: 0
Reputation: 261
I dont know why, but i think my story will help someone who facing the same problem. My problem is just because I forgot to downgrade my Wamp php version, its php 7.1. Looks like my CI not support php 7.1 or higher. So just a few click to downgrade my php, my problem solved.
Upvotes: 4
Reputation: 31
I added session_start() in the main index.php file. Worked like a charm afterwards.
Upvotes: 3
Reputation: 17586
hmm try add to ci->session
and then get data
//assign the CodeIgniter object to a variable
function __construct() {
parent::__construct ();
$this->ci = & get_instance ();
}
//add data to ci session
function test(){
$this->ci->session->set_userdata($user)
}
in the other controller assign the CodeIgniter
object to a variable
function __construct() {
parent::__construct ();
$this->ci = & get_instance ();
}
and get the ci user data
$this->ci->session->all_userdata()
Upvotes: 0
Reputation: 36551
try this..
$user = $this->user_model->user($email, $password);
$user["logged_in"] = TRUE;
$this->session->set_userdata('user',$user); //set session of users with a name user.
to get the session value u can do..
print_r($this->session->userdata('user')); // prints the user session array..
read more about sessions in CI
Upvotes: 1
Reputation: 3252
The set_userdata()
function does not have a return value, so it is correct to display null
on var_dump()
.
In order to save custom data to user's session you should check the manual here
The parameter it accepts must be an array.
Upvotes: 0