Reputation: 6344
I have a JavaScript setInterval()
which is pinging the server every 15 seconds or so.
The problem is that prolongs the user session artificially, as the user their self might be idle.
What is the best way to get the information I'd get from session_start()
without touching the actual session file? In other words, my call would have no effect on extending the session's expiration.
Upvotes: 0
Views: 62
Reputation: 6344
This can be done:
$session = $_COOKIE['PHPSESSID']; //obvs. you must know the name
$path = ini_get('session.save_path') . '/sess_' . $session; //your session file syntax may vary
// now read the file. Note this doesn't touch the file open time, I've tested it
$string = implode('', file($path));
// parsing session string syntax to array looks complex, so do this:
$fp = fopen( substr($path, 0, strlen($path) - 3) . '---', 'w' );
fwrite($fp, $string);
fclose($fp);
// open the created file and let PHP parse it:
session_id( substr($session, 0, strlen($session) - 3) . '---' );
session_start();
// now we correct the Set-Cookie header that would contain dashes at the end
header_remove('Set-Cookie');
setcookie('PHPSESSID', $session);
// and, we're good to go having read the session file but not touched/affected it's expiry time!
Obviously I would prefer to do something like session_start(['read_without_affecting_expiry' => true])
but I don't think that's available, and I want that in only certain instances.
Upvotes: 1