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
More Session Handler Issues
Moderator: General Moderators
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: More Session Handler Issues
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:
Then you should be able to access $_SESSION['cart']
Code: Select all
session_decode('cart|s:15:"this is my cart";');mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.