views:

101

answers:

1

Hey everyone,

I have recently gone into extending my site using node.js and have come to realisation I need a session handler for my PHP sessions. Now everything was cool and dandy and node.js reads the php sessions and can propogate it's own session with the php ones. I am using database sessions so the session data gets saved into a field in the database.

I have however found a slight problem. I am attempting to read the session data into node.js and it's really quite a strange string. I have been able to get the strucutre of each session variable down to:

'field_name'|'type':'length':'value';

Now on certain strings the value field can be missing on other strings the length can be missing (when a variable is Null). The type can also be more than b, s, i; it can also be N (NULL).

I had originally thought up of a huge translator for JS but this just somehow seems a very wrong way to do it.

Has anyone here tried to extract php session variables in JS before and is there any kind of script that could help? Maybe there is a formatting thing I can use on PHP side to make my life a lot easier in node.js?

Edit: the Schema looks like:

{ _id: { id: 'L:\u00c1\u009d\u008e\u00ad\u000e}<\u0002\u0000\u0000' }
, session_id: 'a2clfnjhopv1srs5k5elgbfjv5'
, user_id: 0
, session_data:     'logged|b:0;uid|i:0;server_key|N;AUTH_TIER2|b:0;email|s:0:"";cheese|s:6:"cheese";'
, active: 1
, expires: 1278920567
}

This is the mongo db record for a user session. The field needing to be translated is session_data. There is some kind of formatting error when pasting it in since stackoverflow wont format that as code when I try and make it for some reason.

I tried to JSONfy the field before but it lost it's types and didn't read Null entries etc so I stopped that

Thanks,

+2  A: 

I'm relatively sure PHP uses the serialize and unserialize functions to handle session data.

There is a JavaScript implementation of unserialize in PHP.JS, you can find it here: http://phpjs.org/functions/unserialize

Jani Hartikainen
That is one sexy script Ima try it out. I saw they got serialize as well so I will try them out. Do I need php.js to use these scripts or just the dependancies they state?
Sammaye
I haven't used PHP.JS myself, but I think you can just pick the specific functions you want and their deps if any
Jani Hartikainen
@Jani I am not sure this is handled with serialize/unserialize but rather with session_encode/session_decode
Gordon
You may be right Gordon. Doesn't look like there is a JS implementation fo that yet - @Sammaye your best bet would probably be to see if you can get it functioning with unserialize, since the format is pretty similar but not exactly 1-to-1, and if you run into problems modify the function to behave more like session_decode (less work than coding from scratch)
Jani Hartikainen
Yea, it seems that if you add the 'fieldname'|'serialisedarray' like that it works 1to1 with JS. Need to do a little more testing before I can say it works and I need to store objects within a array so they can be ignored but not deleted by node.js but it does seem as though it could work :D
Sammaye
I just noticed I cna php in node.js...if it communicates directly to php I might be able to run a quick unserialize function
Sammaye
Yes this worked perfect I was able to make a node.js php session handler and it works like a beast thanks :)
Sammaye