note 56033 modified in function.session-set-save-handler by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 16:19:08 +0000 |
| Subject: | note 56033 modified in function.session-set-save-handler by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176046@lists.php.net to get a copy of this message | ||
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.
--was--
Ok, after much hairpulling, I've figured out a successful way to store objects in a session
using a postgresql savehandler.
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.
http://php.net/manual/en/function.session-set-save-handler.php