Procházet zdrojové kódy

Session::$autoUpdateTimestamp

walkor před 3 roky
rodič
revize
7eeb62ee06

+ 14 - 6
src/Protocols/Http/Session.php

@@ -45,6 +45,13 @@ class Session
     public static $name = 'PHPSID';
 
     /**
+     * Auto update timestamp.
+     *
+     * @var bool
+     */
+    public static $autoUpdateTimestamp = false;
+
+    /**
      * Session lifetime.
      *
      * @var int
@@ -298,6 +305,8 @@ class Session
             } else {
                 static::$_handler->write($this->_sessionId, \serialize($this->_data));
             }
+        } elseif (static::$autoUpdateTimestamp) {
+            static::refresh();
         }
         $this->_needSave = false;
     }
@@ -385,15 +394,12 @@ class Session
     }
 
     /**
-     * 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);
     }
 
@@ -405,7 +411,9 @@ class Session
     public function __destruct()
     {
         $this->save();
-        $this->tryGcSessions();
+        if (\rand(1, static::$gcProbability[1]) >= static::$gcProbability[0]) {
+            $this->gc();
+        }
     }
 
     /**

+ 7 - 1
src/Protocols/Http/Session/FileSessionHandler.php

@@ -14,6 +14,8 @@
 
 namespace Workerman\Protocols\Http\Session;
 
+use Workerman\Protocols\Http\Session;
+
 /**
  * Class FileSessionHandler
  * @package Workerman\Protocols\Http\Session
@@ -73,8 +75,12 @@ class FileSessionHandler implements SessionHandlerInterface
         $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 : '';
+            return $data ?: '';
         }
         return '';
     }