Skip to content

frontdevops/expressjs-php-session-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP implementation of Node.js Express Session Handler

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 :)

About

PHP implementation of Node.js Express Session Handler

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages