|
@@ -15,6 +15,21 @@
|
|
|
namespace Workerman\Protocols\Http\Session;
|
|
namespace Workerman\Protocols\Http\Session;
|
|
|
|
|
|
|
|
use Workerman\Protocols\Http\Session;
|
|
use Workerman\Protocols\Http\Session;
|
|
|
|
|
+use function clearstatcache;
|
|
|
|
|
+use function file_get_contents;
|
|
|
|
|
+use function file_put_contents;
|
|
|
|
|
+use function filemtime;
|
|
|
|
|
+use function glob;
|
|
|
|
|
+use function is_dir;
|
|
|
|
|
+use function is_file;
|
|
|
|
|
+use function mkdir;
|
|
|
|
|
+use function rename;
|
|
|
|
|
+use function session_save_path;
|
|
|
|
|
+use function strlen;
|
|
|
|
|
+use function sys_get_temp_dir;
|
|
|
|
|
+use function time;
|
|
|
|
|
+use function touch;
|
|
|
|
|
+use function unlink;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Class FileSessionHandler
|
|
* Class FileSessionHandler
|
|
@@ -41,9 +56,9 @@ class FileSessionHandler implements SessionHandlerInterface
|
|
|
*/
|
|
*/
|
|
|
public static function init()
|
|
public static function init()
|
|
|
{
|
|
{
|
|
|
- $savePath = @\session_save_path();
|
|
|
|
|
|
|
+ $savePath = @session_save_path();
|
|
|
if (!$savePath || str_starts_with($savePath, 'tcp://')) {
|
|
if (!$savePath || str_starts_with($savePath, 'tcp://')) {
|
|
|
- $savePath = \sys_get_temp_dir();
|
|
|
|
|
|
|
+ $savePath = sys_get_temp_dir();
|
|
|
}
|
|
}
|
|
|
static::sessionSavePath($savePath);
|
|
static::sessionSavePath($savePath);
|
|
|
}
|
|
}
|
|
@@ -73,13 +88,13 @@ class FileSessionHandler implements SessionHandlerInterface
|
|
|
public function read(string $sessionId): string
|
|
public function read(string $sessionId): string
|
|
|
{
|
|
{
|
|
|
$sessionFile = static::sessionFile($sessionId);
|
|
$sessionFile = static::sessionFile($sessionId);
|
|
|
- \clearstatcache();
|
|
|
|
|
- if (\is_file($sessionFile)) {
|
|
|
|
|
- if (\time() - \filemtime($sessionFile) > Session::$lifetime) {
|
|
|
|
|
- \unlink($sessionFile);
|
|
|
|
|
|
|
+ clearstatcache();
|
|
|
|
|
+ if (is_file($sessionFile)) {
|
|
|
|
|
+ if (time() - filemtime($sessionFile) > Session::$lifetime) {
|
|
|
|
|
+ unlink($sessionFile);
|
|
|
return '';
|
|
return '';
|
|
|
}
|
|
}
|
|
|
- $data = \file_get_contents($sessionFile);
|
|
|
|
|
|
|
+ $data = file_get_contents($sessionFile);
|
|
|
return $data ?: '';
|
|
return $data ?: '';
|
|
|
}
|
|
}
|
|
|
return '';
|
|
return '';
|
|
@@ -91,10 +106,10 @@ class FileSessionHandler implements SessionHandlerInterface
|
|
|
public function write(string $sessionId, string $sessionData): bool
|
|
public function write(string $sessionId, string $sessionData): bool
|
|
|
{
|
|
{
|
|
|
$tempFile = static::$sessionSavePath . uniqid(bin2hex(random_bytes(8)), true);
|
|
$tempFile = static::$sessionSavePath . uniqid(bin2hex(random_bytes(8)), true);
|
|
|
- if (!\file_put_contents($tempFile, $sessionData)) {
|
|
|
|
|
|
|
+ if (!file_put_contents($tempFile, $sessionData)) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
- return \rename($tempFile, static::sessionFile($sessionId));
|
|
|
|
|
|
|
+ return rename($tempFile, static::sessionFile($sessionId));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -103,21 +118,21 @@ class FileSessionHandler implements SessionHandlerInterface
|
|
|
* @see https://www.php.net/manual/en/class.sessionupdatetimestamphandlerinterface.php
|
|
* @see https://www.php.net/manual/en/class.sessionupdatetimestamphandlerinterface.php
|
|
|
* @see https://www.php.net/manual/zh/function.touch.php
|
|
* @see https://www.php.net/manual/zh/function.touch.php
|
|
|
*
|
|
*
|
|
|
- * @param string $id Session id.
|
|
|
|
|
|
|
+ * @param string $sessionId Session id.
|
|
|
* @param string $data Session Data.
|
|
* @param string $data Session Data.
|
|
|
*
|
|
*
|
|
|
* @return bool
|
|
* @return bool
|
|
|
*/
|
|
*/
|
|
|
- public function updateTimestamp(string $id, string $data = ""): bool
|
|
|
|
|
|
|
+ public function updateTimestamp(string $sessionId, string $data = ""): bool
|
|
|
{
|
|
{
|
|
|
- $sessionFile = static::sessionFile($id);
|
|
|
|
|
|
|
+ $sessionFile = static::sessionFile($sessionId);
|
|
|
if (!file_exists($sessionFile)) {
|
|
if (!file_exists($sessionFile)) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
// set file modify time to current time
|
|
// set file modify time to current time
|
|
|
- $setModifyTime = \touch($sessionFile);
|
|
|
|
|
|
|
+ $setModifyTime = touch($sessionFile);
|
|
|
// clear file stat cache
|
|
// clear file stat cache
|
|
|
- \clearstatcache();
|
|
|
|
|
|
|
+ clearstatcache();
|
|
|
return $setModifyTime;
|
|
return $setModifyTime;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -135,8 +150,8 @@ class FileSessionHandler implements SessionHandlerInterface
|
|
|
public function destroy(string $sessionId): bool
|
|
public function destroy(string $sessionId): bool
|
|
|
{
|
|
{
|
|
|
$sessionFile = static::sessionFile($sessionId);
|
|
$sessionFile = static::sessionFile($sessionId);
|
|
|
- if (\is_file($sessionFile)) {
|
|
|
|
|
- \unlink($sessionFile);
|
|
|
|
|
|
|
+ if (is_file($sessionFile)) {
|
|
|
|
|
+ unlink($sessionFile);
|
|
|
}
|
|
}
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
@@ -146,10 +161,10 @@ class FileSessionHandler implements SessionHandlerInterface
|
|
|
*/
|
|
*/
|
|
|
public function gc(int $maxLifetime): bool
|
|
public function gc(int $maxLifetime): bool
|
|
|
{
|
|
{
|
|
|
- $timeNow = \time();
|
|
|
|
|
- foreach (\glob(static::$sessionSavePath . static::$sessionFilePrefix . '*') as $file) {
|
|
|
|
|
- if (\is_file($file) && $timeNow - \filemtime($file) > $maxLifetime) {
|
|
|
|
|
- \unlink($file);
|
|
|
|
|
|
|
+ $timeNow = time();
|
|
|
|
|
+ foreach (glob(static::$sessionSavePath . static::$sessionFilePrefix . '*') as $file) {
|
|
|
|
|
+ if (is_file($file) && $timeNow - filemtime($file) > $maxLifetime) {
|
|
|
|
|
+ unlink($file);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
return true;
|
|
return true;
|
|
@@ -175,12 +190,12 @@ class FileSessionHandler implements SessionHandlerInterface
|
|
|
public static function sessionSavePath(string $path): string
|
|
public static function sessionSavePath(string $path): string
|
|
|
{
|
|
{
|
|
|
if ($path) {
|
|
if ($path) {
|
|
|
- if ($path[\strlen($path) - 1] !== DIRECTORY_SEPARATOR) {
|
|
|
|
|
|
|
+ if ($path[strlen($path) - 1] !== DIRECTORY_SEPARATOR) {
|
|
|
$path .= DIRECTORY_SEPARATOR;
|
|
$path .= DIRECTORY_SEPARATOR;
|
|
|
}
|
|
}
|
|
|
static::$sessionSavePath = $path;
|
|
static::$sessionSavePath = $path;
|
|
|
- if (!\is_dir($path)) {
|
|
|
|
|
- \mkdir($path, 0777, true);
|
|
|
|
|
|
|
+ if (!is_dir($path)) {
|
|
|
|
|
+ mkdir($path, 0777, true);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
return $path;
|
|
return $path;
|