我有一个JavaScript setInterval()
,它每15秒左右对服务器执行一次ping操作。
问题是人为地延长了用户会话的时间,因为用户自己可能闲置。
在不接触实际会话文件的情况下从session_start()
获取信息的最佳方法是什么?换句话说,我的通话不会延长会话的有效期。
答案 0 :(得分:1)
这可以做到:
$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!
很明显,我更愿意做类似session_start(['read_without_affecting_expiry' => true])
的事情,但我认为这不可用,我只想在某些情况下这样做。