I´m trying to setup a custom session safe handler which works so far. But I don´t want to store the data in PHP session format but pure JSON format. How do I have to change read and write functions to en-/decode the php session data to json.
function write($sessionId, $data) {
$db = new PDO("mysql:host=myhost;dbname=mydb", "myuser", "mypassword");
$sql = "INSERT INTO session SET session_id =" . $db->quote($sessionId) . ", session_data =" . $db->quote($data) . " ON DUPLICATE KEY UPDATE session_data =" . $db->quote($data);
$db->query($sql)
}
function read($sessionId) {
$db = new PDO("mysql:host=myhost;dbname=mydb", "myuser", "mypassword");
$sql = "SELECT session_data FROM session where session_id =" . $db->quote($sessionId);
$result = $db->query($sql);
$data = $result->fetchColumn();
$result->closeCursor();
return $data;
}
The functions are from following tutorial. The author says:
PHP passes the data in serialized to the write function, and expects it serialized back from the read function, but that doesn’t mean you have to store it that way. You could unserialize the data immediately in the write callback and then perform some action dependent on the data or store it however you wish.
I´ve tried following functions which I thought would do the job?
- json_encode()
- json_decode()
- serialize()
- unserialize()
EDIT
If I´m not trying to json_encode the data, it´s written into db as expeceted in following format:
auth|b:1;LAST_ACTIVITY|i:1407480463;