Kevin Sedgley
Kevin Sedgley

Reputation: 1069

Node.js and PHP session sharing

I am trying to share session information between our main PHP app and node.js

We currently use a database backend for session storage, because we have several web servers that can handle a request.

Is anyone aware of any solutions for accessing session data from node.js? The only ones I can see use redis or memcache, but we cannot change the method for storing session data.

Upvotes: 3

Views: 654

Answers (1)

Ronen Botzer
Ronen Botzer

Reputation: 7117

You would have to use a common format, such as JSON or one of your own invention. Let's assume JSON for convenience.

On the PHP side you will need to register your own session handler.

Set session.name to something less PHP specific, for example SESSID instead of PHPSESSID.

Set session.serialize_handler to php_serialize. In the write() method cast the incoming $data into JSON by first calling unserialize() on it, then calling json_encode() and set the re-encoded data in your database.

Similarly, the read() method should extract the session data from your database (given the value of the ID stored in the SESSID cookie), call json_decode() on it, then serialize it and return that string.

Might I suggest a fast, scalable, and reliable database such as Aerospike. I am, however, biased.

Upvotes: 2

Related Questions