rancoud / session
会话包
5.0.8
2024-09-02 12:12 UTC
Requires
- php: >=7.4.0
- ext-mbstring: *
- ext-openssl: *
- ext-session: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16 || ^3.0
- phpunit/phpunit: ^9.1 || ^10.0 || ^11.0
- predis/predis: ^1.1 || ^2.0
- rancoud/database: ^6.0
- squizlabs/php_codesniffer: ^3.5
README
Session.
安装
composer require rancoud/session
信息
默认情况下,会话为只读(选项 read_and_close = 1)。
您可以使用 Session::setReadWrite()
或 Session::setReadOnly()
指定。
不需要调用 Session::start()
,但
- 当使用
get, has, hasKeyAndValue, getAll
时,会话将自动以只读模式启动。 - 当使用
set, remove, getAndRemove, keepFlash, gc, regenerate
时,会话将自动以写入模式启动。
如何使用它?
从 $_SESSION 设置和获取值
Session::set('key', 'value'); $value = Session::get('key');
使用自定义选项
// first way Session::setOption('name', 'custom_session_name'); // second way Session::start(['cookie_lifetime' => 1440]); Session::set('key', 'value'); $value = Session::get('key');
在默认 PHP 会话上启用加密
Session::useDefaultEncryptionDriver('keyForEncryption'); Session::set('key', 'value'); $value = Session::get('key');
使用文件驱动器
Session::useFileDriver(); Session::set('key', 'value'); $value = Session::get('key');
使用数据库驱动器(您必须安装 rancoud/database)
$conf = new \Rancoud\Database\Configurator([ 'engine' => 'mysql', 'host' => '127.0.0.1', 'user' => 'root', 'password' => '', 'database' => 'test_database' ]); $db = new \Rancoud\Database\Database($conf); // for using a current connection Session::useCurrentDatabaseDriver($db); // for creating a new connection //Session::useNewDatabaseDriver($conf); Session::set('key', 'value'); $value = Session::get('key');
使用 Redis 驱动器(您必须安装 predis/predis)
$params = [ 'scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379, ]; $redis = new Predis\Client($params); // for using a current connection Session::useCurrentRedisDriver($redis); // for creating a new connection //Session::useNewRedisDriver($params); Session::set('key', 'value'); $value = Session::get('key');
使用您自己的实现 SessionHandlerInterface
和/或 SessionUpdateTimestampHandlerInterface
的驱动器
$driver = new MyCustomDriver(); Session::useCustomDriver($driver); Session::set('key', 'value'); $value = Session::get('key');
Session
静态通用命令
- start([options: array = []]): void
- regenerate(): bool
- destroy(): bool
- commit(): void
- rollback(): bool
- unsaved(): bool
- hasStarted(): bool
- getId(): string
- setId(id: string): string
- gc(): void
- setReadOnly(): void
- setReadWrite(): void
- isReadOnly(): bool
静态变量 $_SESSION 访问
- set(key: string, value: mixed): void
- get(key: string): mixed
- getAll(): array
- getAndRemove(key: string): mixed
- has(key: string): bool
- hasKeyAndValue(key: string, value: mixed): bool
- remove(key: string): void
静态变量闪存访问
闪存数据存储在单独的变量中。
它们将在脚本执行结束时或 commit()
unsaved()
之后消失。
您可以使用 keepFlash 将其保存到 $_SESSION 中。
当恢复闪存数据时,它将在 $_SESSION 中被删除。
- setFlash(key: string, value: mixed): void
- getFlash(key: string): mixed
- getAllFlash(): array
- hasFlash(key: string): bool
- hasFlashKeyAndValue(key: string, value: mixed): bool
- keepFlash([keys: array = []]): void
静态选项
- setOption(key: string, value): void
- setOptions(options: array): void
- getOption(key: string): mixed
静态驱动器
- getDriver(): \SessionHandlerInterface
静态 PHP 会话默认驱动器
- useDefaultDriver(): void
- useDefaultEncryptionDriver(key: string, [method: string|null = null]): void
- setLengthSessionID(length: int): void
- getLengthSessionID(): int
静态文件驱动器
- useFileDriver(): void
- useFileEncryptionDriver(key: string, [method: string|null = null]): void
- setPrefixForFile(prefix: string): void
- setLengthSessionID(length: int): void
- getLengthSessionID(): int
静态数据库驱动器
- useNewDatabaseDriver(configuration: \Rancoud\Database\Configurator|array): void
- useCurrentDatabaseDriver(databaseInstance: \Rancoud\Database\Database): void
- useNewDatabaseEncryptionDriver(configuration: \Rancoud\Database\Configurator|array, key: string, [method: string = null]): void
- useCurrentDatabaseEncryptionDriver(databaseInstance: \Rancoud\Database\Database, key: string, [method: string = null]): void
- setUserIdForDatabase(userId: int): void
- setLengthSessionID(length: int): void
- getLengthSessionID(): int
静态 Redis 驱动器
- useNewRedisDriver(configuration: array|string): void
- useCurrentRedisDriver(redisInstance: \Predis\Client): void
- useNewRedisEncryptionDriver(configuration: array|string, key: string, [method: string = null]): void
- useCurrentRedisEncryptionDriver(redisInstance: \Predis\Client, key: string, [method: string = null]): void
- setLengthSessionID(length: int): void
- getLengthSessionID(): int
静态自定义驱动器
- useCustomDriver(customDriver: \SessionHandlerInterface): void
会话选项
您可以更改的会话选项列表
- save_path
- name
- 保存处理程序
- 自动启动
- 垃圾回收概率
- 垃圾回收除数
- 垃圾回收最大生命周期
- 序列化处理程序
- cookie生命周期
- cookie路径
- cookie域名
- cookie安全
- cookie仅http
- cookie同站策略
- 使用严格模式
- 使用cookie
- 仅使用cookie
- 检查引用者
- 缓存限制器
- 缓存过期时间
- 使用trans_sid
- trans_sid标签
- trans_sid主机
- sid长度
- 每字符位数
- 上传进度启用
- 上传进度清理
- 上传进度前缀
- 上传进度名称
- 上传进度频率
- 上传进度最小频率
- 延迟写入
- 读取并关闭
驱动程序信息
默认
使用SessionHandler
文件
扩展SessionHandler
数据库
您需要安装
composer require rancoud/database
CREATE TABLE IF NOT EXISTS `sessions` ( `id` varchar(128) NOT NULL, `id_user` int(10) unsigned DEFAULT NULL, `last_access` datetime NOT NULL, `content` text NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Redis
您需要安装
composer require predis/predis
如何开发
docker compose build && docker compose run lib composer ci
用于启动测试