note 56033 added to function.session-set-save-handler

From: Date: Sun, 21 Aug 2005 11:35:55 +0000
Subject: note 56033 added to function.session-set-save-handler
Groups: php.notes 
Request: Send a blank email to php-notes+get-93987@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. 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. ---- Manual Page -- http://www.php.net/manual/en/function.session-set-save-handler.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56033 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56033&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56033&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56033&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56033&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56033&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56033&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56033&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#93987) next »