ghostff/session
Composer 安装命令:
composer require ghostff/session
包简介
PHP Session Manager (non-blocking, flash, segment, session encryption).
README 文档
README
PHP Session Manager (non-blocking, flash, segment, session encryption). Uses PHP open_ssl for optional encrypt/decryption of session data.
Driver Support Scope
Installation
You can download the Latest release version as a standalone, alternatively you can use Composer
composer require ghostff/session
Basic usage
# Start session with default configurations. $session = new Session(); $session->set('email', 'foo@bar.com'); echo $session->get('email');
Configuration Options
# use custom configuration file. Session::setConfigurationFile('path/to/my/config.php'); # overriding specific configuration settings Session::updateConfiguration([ Session::CONFIG_DRIVER => Redis::class, Session::CONFIG_START_OPTIONS => [ Session::CONFIG_START_OPTIONS_SAVE_PATH => __DIR__ . '/tmp' ] ]); # override a configuration for current session instance $session = new Session([Session::CONFIG_ENCRYPT_DATA => true]);
Initializing Session
# Start session with an auto generated id. $session = new Session(); # Start session with custom ID $session = new Session(null, bin2hex(random_bytes(32)));
Using Segment :Session
$segment = $session->segment('my_segment');
Retrieving Session ID :string
echo $session->id();
Committing changes :void
# Opens, writes and closes session. $session->commit();
Setting Session Data :Session
$session->set('fname', 'foo'); # Setting Segment $segment->set('name', 'bar'); # Setting Flash $session->setFlash('name', 'foobar'); # Setting Segment Flash $segment->setFlash('name', 'barfoo'); $session->commit();
Retrieving Session Data :mixed
echo $session->get('name'); # outputs foo echo $session->getOrDefault('unset_value', 'not found'); # outputs not found # Retrieving Segment echo $segment->get('name'); # outputs bar echo $segment->getOrDefault('unset_value', 'not found'); # outputs not found # Retrieving Flash echo $session->getFlash('name'); # outputs foobar echo $session->getFlashOrDefault('name', 'not found'); # outputs not found # Retrieving Segment Flash echo $segment->getFlash('name'); # outputs barfoo echo $segment->getFlashOrDefault('name', 'not found'); # outputs not found
Removing Session Data :Session
$session->del('name'); # Removing Segment $segment->del('name'); # Removing Flash $session->delFlash('name'); # Removing Segment Flash $segment->delFlash('name');
Retrieve all session or segment data :array
$session->getAll(); # Retrieve only in specified segment. $session->getAll('my_segment_name');
Check if variable exist in current session namespace :bool
$session->exist('name'); # Search flashes $session->exist('name', true);
Removing all data in current segment :Session
$session->clear();
Destroying session :void
$session->destroy();
Regenerate session ID :void
$session->rotate(); # Delete the old associated session file or not $session->rotate(true);
Setting Queued Session Data :Session
$session->push('age', 10) ->push('age', 20) ->push('age', 30) ->push('age', 40);
Retrieving (pop/shift) Queued Session Data :mixed
echo $session->pop('age', true); # outputs 10 echo $session->pop('age', true); # outputs 20 echo $session->pop('age'); # outputs 40 echo $session->pop('age'); # outputs 30
Driver configuration
You can provide your own database connection (PDO, Memcached, Redis, etc). Provide the connection before constructing the session:
// Example: supplying a PDO connection $pdo = new PDO('mysql:host=127.0.0.1;dbname=sessions', 'user', 'pass', [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ]); Session::setConnection(Session::CONFIG_MYSQL_DS, $pdo); // Then construct your session (the configured driver will use this connection) $session = new Session();
Similarly, you can set connections for:
Session::CONFIG_SQLITE_DSwith a PDO SQLite connectionSession::CONFIG_MEMCACHED_DSwith a Memcached instanceSession::CONFIG_REDIS_DSwith a Redis instance
ghostff/session 适用场景与选型建议
ghostff/session 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.91k 次下载、GitHub Stars 达 37, 最近一次更新时间为 2017 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「sql」 「memcached」 「session」 「non-blocking」 「segment」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ghostff/session 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ghostff/session 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ghostff/session 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memcached Bundle
Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking
A Symfony bundle for Mutex implementation for PHP
Microservice RPC through message queues.
Graphic stand-alone administration for memcached to monitor and debug purpose
The CodeIgniter Redis package
统计信息
- 总下载量: 2.91k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 38
- 点击次数: 9
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: 0BSD
- 更新时间: 2017-06-24