danfsd/php-express-session-handler

Session Handler that talks the same language as your express-session.

dev-master 2018-10-09 14:17 UTC

This package is not auto-updated.

Last update: 2024-10-05 01:55:18 UTC


README

This library is meant to work as a Session Handler for PHP that is compatible with express-session.

The main motivation for this was the need to share Session between a PHP application and a NodeJS application.

This was inspired on this gist by mscdex, implementing some of its strategies with a SessionHandler implementation.

Requirements

This library is meant to use the Save Handlers defined on session.save_handler directive, the library itself doesn't have any code regarding save handlers.

  • PHP: 5.6 or greater
  • phpredis or other Save Handler Extension. (you can install it using pecl install redis)
  • php_serialize Serialize Handler.

NOTE: this was tested using express-session with the connect-redis Session Store. This is supposed to work with any Session Store that stores it's data as JSON.

Installation

Composer

Just run:

composer require danfsd/php-express-session-handler

Setup

Redis

You can set the following directive on php.ini like the following:

session.session_name = PHPSESSID
session.save_handler = redis
session.save_path = "tcp://127.0.0.1/?prefix=session:php:"
session.serialize_handler = php_serialize

Or you can set it using PHP's ini_set function like the following:

ini_set("session.session_name", "PHPSESSID");
ini_set("session.save_handler", "redis");
ini_set("session.save_path", "tcp://127.0.0.1/?prefix=session:php:");
ini_set("session.serialize_handler", "php_serialize");

Usage

use danfsd\ExpressSessionHandler;

// This is the express-session's secret you defined in your NodeJS application
const SESSION_SECRET = "node.js";

$handler = new ExpressSessionHandler(SESSION_SECRET);

// Setting the Handler
session_set_save_handler($handler, true);

// Starting/Recoverying session
session_start();

// Populates $_SESSION['cookie'] with data that express-session requires
$handler->populateSession();

echo "<pre>";
var_dump($_SESSION);
var_dump($_COOKIE);
echo "</pre>;"

More examples will be disclosed soon.