note 56033 deleted from function.session-set-save-handler by cmb
| From: | cmb@php.net | Date: | Sun, 24 Nov 2019 09:11:03 +0000 |
| Subject: | note 56033 deleted from function.session-set-save-handler by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213239@lists.php.net to get a copy of this message | ||
Note Submitter: ccav at maxbaud dot net
----
Ok, after much hairpulling, I've figured out a successful way to store objects in a session
using a postgresql savehandler.
<?php
function write ($id, $sess_data)
{
global $sql; // my global db connection object
$sess_data = @pg_escape_bytea(serialize($sess_id)); //works with any type
$sql->Query("delete from sessions where sessionid = '$id'"); // delete old
session record
$sql->Query("insert into sessions (sessionid,datavalue) values ('$id',
'$sess_data')"); //insert into session table
return true;
}
function read ($id)
{
global $sql;
$result=$sql->Query("select * from sessions where sessionid = '$id'");
if ($sql->rows==1) {
$row=$sql->Fetch(0);
$rval = unserialize(@pg_unescape_bytea($sql->data[2]));
return $rval;
} else {
return "";
}
}
?>
Make the datavalue column (the one used to store the actual session data) in the sessions table type
bytea.
The problem apparently lies in the PHP serialize for objects, where there is a CR/LF inserted
between the object id and the property array. The CR/LF isn't escaped, and causes a fit for
postgresql.
Any data type in PHP can be serialized, so there's no need to distinguish between types.
The object is fully restored, with methods.