Skip to content

Commit 9c622f3

Browse files
committed
Still update the session on AJAX calls, just don't regenerate the session ID
1 parent eea2ff5 commit 9c622f3

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

system/libraries/Session.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,40 @@ public function sess_create()
334334
public function sess_update()
335335
{
336336
// We only update the session every five minutes by default
337-
if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now
338-
OR $this->CI->input->is_ajax_request()) // Changing the session ID during an AJAX call causes problems
337+
if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)
339338
{
340339
return;
341340
}
342341

342+
// _set_cookie() will handle this for us if we aren't using database sessions
343+
// by pushing all userdata to the cookie.
344+
$cookie_data = NULL;
345+
346+
/* Changing the session ID during an AJAX call causes problems,
347+
* so we'll only update our last_activity
348+
*/
349+
if ($this->CI->input->is_ajax_request())
350+
{
351+
$this->userdata['last_activity'] = $this->now;
352+
353+
// Update the session ID and last_activity field in the DB if needed
354+
if ($this->sess_use_database === TRUE)
355+
{
356+
// set cookie explicitly to only have our session data
357+
$cookie_data = array();
358+
foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
359+
{
360+
$cookie_data[$val] = $this->userdata[$val];
361+
}
362+
363+
$this->CI->db->query($this->CI->db->update_string($this->sess_table_name,
364+
array('last_activity' => $this->userdata['last_activity']),
365+
array('session_id' => $this->userdata['session_id'])));
366+
}
367+
368+
return $this->_set_cookie($cookie_data);
369+
}
370+
343371
// Save the old session id so we know which record to
344372
// update in the database if we need it
345373
$old_sessid = $this->userdata['session_id'];
@@ -357,10 +385,6 @@ public function sess_update()
357385
$this->userdata['session_id'] = $new_sessid = md5(uniqid($new_sessid, TRUE));
358386
$this->userdata['last_activity'] = $this->now;
359387

360-
// _set_cookie() will handle this for us if we aren't using database sessions
361-
// by pushing all userdata to the cookie.
362-
$cookie_data = NULL;
363-
364388
// Update the session ID and last_activity field in the DB if needed
365389
if ($this->sess_use_database === TRUE)
366390
{

0 commit comments

Comments
 (0)