I created a simple log in system, and i used CI Sessions for holding some values.
But CI sessions did’t work as expected, it does not retrieve any value.
I tried to track the problem, and i figured out something:
Whenever i refresh the page, a new session is created!
And only 1 session hold my custom variables(is_logged, username).
Other sessions just hold default data.
I tried holding the sessions in a database, and still the same problem.
Is it my encryption key?
I just used bunch of random letters, because i can’t keep it empty.
This is the controller I tested my sessions on.
Note:
I’m using some echo‘s here, just for testing.
Note2:
I can retrive session variables only if i call it on the same page.
<?php
class site extends CI_Controller {
function index() {
if (!$this->session->userdata('is_logged')) {
$data['main_content'] = 'site_view';
$this->load->view('template/all_templates', $data);
} else {
echo 'you\'re logged already';
}
}
function login() {
if (!$this->session->userdata('is_logged')) {
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($username && $password) {
if ($this->site_model->validate($username, $password)) {
$user_data = array('username' => $username, 'is_logged' => TRUE);
$this->session->set_userdata($user_data);
echo 'you successfully log in.';
redirect('site/index', 'refresh');
} else {
echo 'cannot login';
}
} else {
echo 'username and/or password not set';
}
} else {
echo 'you\'re logged in already!';
}
}
}
?>
Note:
This question is related to this one
Edit1:
I logged in few times, sometimes with no errors.
After few refreshes/log ins, see sesssion database:
02d6e3521046ffe7df36b07e88624a60 127.0.0.1 Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.2 (K 1323803371
66a9cf3d6b7e0521dcc7e5bd3d5d4ad1 127.0.0.1 Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.2 (K 1323803380
923a78db029ee3e3ae4bf9e09873fcae 127.0.0.1 Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.2 (K 1323803366 a:3:{s:9:"user_data";s:0:"";s:8:"username";s:4:"aziz";s:9:"is_logged";b:1;}
99ac075792b7818aadac60d00ca46947 127.0.0.1 Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.2 (K 1323803367
bbe10f3338226c8d83b0ae58d1ca5149 127.0.0.1 Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.2 (K 1323803343 a:3:{s:9:"user_data";s:0:"";s:8:"username";s:4:"aziz";s:9:"is_logged";b:1;}
ed6a22ae1d2fd21b6ef66668c2078a11 127.0.0.1 Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.2 (K 1323803375
feb5806176dc8a4f7ed8f30763f65f8b 127.0.0.1 Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.2 (K 1323803380 a:3:{s:9:"user_data";s:0:"";s:8:"username";s:4:"aziz";s:9:"is_logged";b:1;}
As you may notice, some results with user data.
my validate function :
function validate($username, $password) {
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if ($query->num_rows() == 1) {
return true;
} else {
return false;
}
}
Note:
I tested in FF & Chrome.
Aziz a couple of months ago I suffered a lot of troubles with the original CI session library (included what you mention). Finally I arrived to this replacement that use
native PHP session. It works!Believe me, in the middle of a project, I did not stop to wonder why. Just works.
Here it is: Codeigniter’s Native session (there is a download link at the bottom)
BUT, due that it is an old library you MUST made some hacks. You can check those simple hacks in the library’s forum
Just drop this file in codeigniter’s library directory. And let’s talk about forward compatibility after, I mean it! 🙂