Skip to content

Commit

Permalink
Session::$autoUpdateTimestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed May 15, 2022
1 parent 3462e45 commit 17937eb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
20 changes: 14 additions & 6 deletions Protocols/Http/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class Session
*/
public static $name = 'PHPSID';

/**
* Auto update timestamp.
*
* @var bool
*/
public static $autoUpdateTimestamp = false;

/**
* Session lifetime.
*
Expand Down Expand Up @@ -297,6 +304,8 @@ public function save()
} else {
static::$_handler->write($this->_sessionId, \serialize($this->_data));
}
} elseif (static::$autoUpdateTimestamp) {
static::refresh();
}
$this->_needSave = false;
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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();
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions Protocols/Http/Session/FileSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
namespace Workerman\Protocols\Http\Session;

use Workerman\Protocols\Http\Session;

/**
* Class FileSessionHandler
* @package Workerman\Protocols\Http\Session
Expand Down Expand Up @@ -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 : '';
}
Expand Down

0 comments on commit 17937eb

Please sign in to comment.