Coder Social home page Coder Social logo

php-express-session-handler's Introduction

Express Session Handler

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.