diff --git a/Protocols/Http/Session.php b/Protocols/Http/Session.php index 6ae77e63d..db5b2a2f5 100644 --- a/Protocols/Http/Session.php +++ b/Protocols/Http/Session.php @@ -43,6 +43,13 @@ class Session */ public static $name = 'PHPSID'; + /** + * Auto update timestamp. + * + * @var bool + */ + public static $autoUpdateTimestamp = false; + /** * Session lifetime. * @@ -297,6 +304,8 @@ public function save() } else { static::$_handler->write($this->_sessionId, \serialize($this->_data)); } + } elseif (static::$autoUpdateTimestamp) { + static::refresh(); } $this->_needSave = false; } @@ -384,15 +393,12 @@ protected static function initHandler() } /** - * Try GC sessions. + * GC sessions. * * @return void */ - public function tryGcSessions() + public function gc() { - if (\rand(1, static::$gcProbability[1]) > static::$gcProbability[0]) { - return; - } static::$_handler->gc(static::$lifetime); } @@ -404,7 +410,9 @@ public function tryGcSessions() public function __destruct() { $this->save(); - $this->tryGcSessions(); + if (\rand(1, static::$gcProbability[1]) >= static::$gcProbability[0]) { + $this->gc(); + } } /** diff --git a/Protocols/Http/Session/FileSessionHandler.php b/Protocols/Http/Session/FileSessionHandler.php index 5c862a5e5..a6211a25c 100644 --- a/Protocols/Http/Session/FileSessionHandler.php +++ b/Protocols/Http/Session/FileSessionHandler.php @@ -13,6 +13,8 @@ */ namespace Workerman\Protocols\Http\Session; +use Workerman\Protocols\Http\Session; + /** * Class FileSessionHandler * @package Workerman\Protocols\Http\Session @@ -70,6 +72,10 @@ public function read($session_id) $session_file = static::sessionFile($session_id); \clearstatcache(); if (\is_file($session_file)) { + if (\time() - \filemtime($session_file) > Session::$lifetime) { + \unlink($session_file); + return ''; + } $data = \file_get_contents($session_file); return $data ? $data : ''; }