geekjob / expressjs-php-session-handler
与Node.js express-session处理器兼容的会话处理器
1.2.2
2022-07-15 06:11 UTC
Requires
- php: >=7.2
This package is not auto-updated.
Last update: 2024-09-29 18:37:20 UTC
README
这个库旨在作为PHP的会话处理器,与express-session兼容。
这个库是为SOA(面向服务的架构)项目编写的,用于在Node.js应用程序和用PHP编写的服务之间共享会话。
要求
- PHP:
8.1
或更高版本 - phpredis
- php_serialize 标准PHP序列化处理器。
这个库现在支持使用express-session与connect-redis会话存储作为通用会话存储。
安装
安装Redis
您可以使用pecl install redis
来安装它
在Docker文件中
FROM php:fpm ... RUN pecl install redis && docker-php-ext-enable redis
Composer
只需运行即可从packagist.org获取包
composer require geekjob/expressjs-php-session-handler
配置和使用
配置Node.js应用程序
app.use(session({ name: 'sid', secret: 'secret key', cookie: { // Share cookie through sub domains if you use many domains for service architecture domain : '.your.domain', maxAge : Date.now() + 60000 }, store: new RedisStore({ host : 'redis', port : 6379, client: redis, prefix: 'session:', ttl : 3600 // 60 min }) }));
配置运行时
require_once 'vendor/autoload.php'; \GeekJOB\ExpressjsSessionHandler::register( name : 'sid', secret: 'secret key', cookie: [ 'domain' => '.your.domain', // Share cookie through sub domains 'path' => '/', 'maxage' => strtotime('+1hour')-time(), // Set maxage ], store : [ 'handler' => 'redis', 'path' => 'tcp://127.0.0.1:6379', 'prefix' => 'session:', 'ttl' => 3600 // 60 min ], secure: false // Set to true if signature verification is needed. );
或者使用如下列表方式进行配置
\GeekJOB\ExpressjsSessionHandler::register( [ 'name' => 'sid', 'secret' => 'secret key', 'cookie' => [ 'domain' => '.your.domain', // Share cookie through sub domains 'path' => '/', 'maxage' => strtotime('+1hour')-time(), // Set maxage ], 'store' => [ 'handler' => 'redis', 'path' => 'tcp://127.0.0.1:6379', 'prefix' => 'session:', 'ttl' => 3600 // 60 min ], 'secure' => false // Set to true if signature verification is needed. ] );
通过php.ini文件配置生产服务器
session.session_name = sid session.save_handler = redis session.save_path = "tcp://127.0.0.1/?prefix=session:" session.serialize_handler = php_serialize ; After this number of seconds, stored data will be seen as 'garbage' and ; cleaned up by the garbage collection process. ; https://php.ac.cn/session.gc-maxlifetime ; default: session.gc_maxlifetime = 1440 ; Redis Sessions use this value for setting TTL session.gc_maxlifetime = ; Lifetime in seconds of cookie or, if 0, until browser is restarted. ; https://php.ac.cn/session.cookie-lifetime session.cookie_lifetime =
require_once 'vendor/autoload.php'; \GeekJOB\ExpressjsSessionHandler::register( secret: 'secret key', cookie: [ 'domain' => '.your.domain', // Share cookie through sub domains 'path' => '/', ] );
待办事项
- MongodDB支持
- Dragonfly支持
- 单元测试 :)