geekjob / expressjs-php-session-handler
Session handler compatible with Node.js express-session handler
1.2.2
2022-07-15 06:11 UTC
Requires
- php: >=7.2
This package is not auto-updated.
Last update: 2024-11-10 19:10:27 UTC
README
This library is meant to work as a Session Handler for PHP that is compatible with express-session.
This library writed for SOA (Service-oriented architecture) projects for sharing session between a Node.js application and services writed on PHP.
Requirements
- PHP:
8.1
or greater - phpredis
- php_serialize Standart PHP Serialize Handler.
This library now created for using express-session with the connect-redis Session Store as common session store.
Installation
Install Redis
You can install it using pecl install redis
In Docker file:
FROM php:fpm ... RUN pecl install redis && docker-php-ext-enable redis
Composer
Just run to get the package from packagist.org:
composer require geekjob/expressjs-php-session-handler
Setup and Usage
Configure Node.js application
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 }) }));
Configure runtime
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. );
or use list like way for config
\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. ] );
Configure for production server via php.ini file
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. ; http://php.net/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. ; http://php.net/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' => '/', ] );
TODO
- MongodDB support
- Dragonfly support
- Unit tests :)