ddrv / slim-session
PHP会话库
v2.0.1
2020-12-25 11:05 UTC
Requires
- php: >=7.1
- ext-mbstring: *
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- ext-openssl: *
- phpunit/phpunit: ^9.1
- slim/psr7: ^1.0
- squizlabs/php_codesniffer: ^3.5
Suggests
- ext-openssl: Need for using Ddrv\Slim\Session\Handler\EncryptedHandlerDecorator
README
ddrv/slim-session
PHP库,用于处理会话。
安装
- 在控制台运行
composer require ddrv/slim-session:^2.0
- 包含自动加载文件
require_once('vendor/autoload.php');
集成到旧代码中
例如,会话cookie名称使用为
sess_id
。
-
初始化存储和处理程序(例如,
\Ddrv\Slim\Session\Storage\FileHandler
,但可以是任何实现\Ddrv\Slim\Session\Storage
接口的实现)。$storage = new Ddrv\Slim\Session\Storage\FileStorage('/path/to/sessions', 'sess_id'); $handler = new Ddrv\Slim\Session\Handler($storage);
-
定义会话ID并启动会话
/** @var Ddrv\Slim\Session\Handler $handler */ $sessionId = array_key_exists('sess_id', $_COOKIE) ? $_COOKIE['sess_id'] : $handler->generateId(); $session = $handler->read($sessionId); // some logic // When you need to update session ID do $session->regenerate(); // some logic if ($session->isNeedRegenerate()) { $handler->destroy($sessionId); $sessionId = $handler->generateId(); } $handler->write($sessionId, $session); // store data to storage and close session // add session cookie to response setcookie('sess_id', $sessionId, time() + 86400, '/', '.example.com', false, true);
-
当需要销毁会话时,执行以下操作
/** @var string $sessionId */ /** @var Ddrv\Slim\Session\Handler $handler */ $handler->destroy($sessionId); setcookie('sess_id', "", time() + 86400, '/', '.example.com', false, true);
集成到PRS框架中
此包包含Psr\Http\Server\MiddlewareInterface
(PSR-15
)实现。请参阅Ddrv\Slim\Session\Middleware\SessionMiddleware
类。
使用
将会话用作键值存储
/** @var Ddrv\Slim\Session\Session $session */ $session->set('key1', 'value'); $session->set('key2', ['a', 'b', 'c']); $value = $session->get('key1'); // 'value' $value = $session->get('key1', 'default'); // 'value' $value = $session->get('nonexistent-key'); // null $value = $session->get('nonexistent-key', 'default'); // 'default'
使用闪存消息
/** @var Ddrv\Slim\Session\Session $session */ $session->flash('key1', 'value'); // 'key1' will be stored only for the current and the next request // Current request $session->has('key1'); // true $value = $session->get('key1'); // 'value' // Next request $session->has('key1'); // true $value = $session->get('key1'); // 'value' // Other request $session->has('key1'); // false $value = $session->get('key1'); // null
使用计数器
/** @var Ddrv\Slim\Session\Session $session */ $session->increment('counter_1'); // 1 $session->increment('counter_1'); // 2 $session->increment('counter_1'); // 3 $session->increment('counter_1'); // 4 $session->increment('counter_1'); // 5 $session->decrement('counter_1'); // 4 $session->decrement('counter_1'); // 3 $session->decrement('counter_1'); // 2 $session->counter('counter_1'); // 2 $session->reset('counter_1'); // 0 $session->counter('counter_1'); // 0
删除旧会话
定期从存储中删除旧会话。
/** @var Ddrv\Slim\Session\Handler $handler */ $handler->removeExpiredSessions(); // Delete sessions not used during the day
加密
您可以使用加密对会话数据进行加密。使用Ddrv\Slim\Session\Handler\EncryptionHandlerDecorator
进行加密(需要openssl
PHP扩展)。
/** @var Ddrv\Slim\Session\Storage $handler */ $cryptHandler = new Ddrv\Slim\Session\Storage\EncryptedStorageDecorator($handler, 'secret-key', 16);