Page 1 of 1

More Session Handler Issues

Posted: Tue May 11, 2010 9:54 am
by drayfuss
Hey,

I'm currently building a session handler class which not only shoves session data into a mysql database but also ties relevant sessions to a users table. I figured, since sessions and user authentication are so interlinked, that I should power both with the session_set_save_handler functions.

A problem I'm having, is tweaking the session_data that is generated by PHP's session engine. How can I decode this data and use just one or two of the contained assoc arrays contained within it, in order to update a few fields on a completely different database table?

An assoc array like this:

$_SESSION['cart'] = "this is my cart";

looks like this in the database:

cart|s:15:"this is my cart";

But how can I turn it back into an assoc array which PHP can understand so it can pump a specific key/value from the assoc array into my users table?

Just to be clear, I have managed to get the session data back out of the database and into the $_SESSION super-global working nicely. But I don't want to have to wait for the super-global to become re-populated before updating my users table. Hence, I want to pick out some data from the session data before it gets sent back to the $_SESSION super-global. I've tried session_decode() with no avail.

Cheers,

drayfuss

Re: More Session Handler Issues

Posted: Tue May 11, 2010 10:29 am
by AbraCadaver
Firstly, my opinion we be to NOT handle your user authentication in the session handler. This is creating a dependency that may cause future troubles. Second, it will take a kludge to properly extract data from a serialized session, but you should be able to use the session_decode() because it decodes it and sets the session vars. So if you do:

Code: Select all

session_decode('cart|s:15:"this is my cart";');
Then you should be able to access $_SESSION['cart']