1
votes

I have an issue about Codeigniter Sessions. When I logged in to the our system, Session is set. In this session "user_id" also set. After few redirects my "user_id" Session variable is null (no value).

then it redirects to login page due to null value of user_id . Why this issue is happens ?

See the following config.php file

$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;
$config['cookie_prefix']    = "";
$config['cookie_domain']    = "";
$config['cookie_path']      = "/";
$config['cookie_secure']    = FALSE;
$config['csrf_protection'] = FALSE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;

Please help me to overcome this issue. !

2
change $config['sess_expiration'] = 86400 - jitendrapurohit
What version of codeingiter you using? - Mr. ED

2 Answers

1
votes

Replace

$config['sess_expiration']      = 7200;

with

$config['sess_expiration']      = 0;

for more on session read this this and this link

1
votes

You didn't provide much information about your application but if you have a lot of ajax requests in between the redirects, you may be encountering the classic CI 2 bug that there is a race condition that effectively resets user sessions whenever an ajax request was made during the session rotation window.

CodeIgniter stores sessions in a table indexed by a session_id field. This is a unique identifier that is randomly generated both at session creation and rotation. The session_id is sent out to the browser as a cookie after every request, and this overwrites whatever session_id is stored in the cookie. This system works quite well during normal operation when only one request is sent between the client and the server at any time. During normal operation, the same cookie would be exchanged between the client and the server. At session rotation, the server would send a new cookie to the client which would store it and re-send the new cookie back to the server on the next request. This also works properly.

What doesn’t work is when the server receives two simultaneous requests during a session rotation (simultaneous being defined as a request sent to the server before all other in-flight requests are received).

This link here explains it thoroughly and if this is your case then you can find the fix there.

The problem exists because of the way browsers make ajax requests (or at least the way we make ajax requests), and the way CodeIgniter handles session rotation.

PS. Its Codeigniter not codeignitor :)