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

From: Date: Thu, 08 Sep 2011 12:35:40 +0000
Subject: note 105704 added to function.session-set-save-handler
Groups: php.notes 
Request: Send a blank email to php-notes+get-183028@lists.php.net to get a copy of this message
I have run across this problem, where in the read overridden methods data argument is in php internal serialization, which can be changed, to either php_binary or some other type that I don't have installed. The problem is that this internal serializer can't be easily parsed. Would be nice if you could just serialize the $_SESSION object and save it to the database...I did just that, now I have the full $_SESSION object to interrogate outside of the current user session. I'm posting this as I had not found this anywhere else during my searching and research into the session_save_handler method. Here is my full implementation, must be run after database connections, use the $_SESSION object as you would normally. session_write_close(); ini_set("session.serialize_handler", "php"); session_set_save_handler('_open', '_close', '_read', '_write', '_destroy', '_clean'); function _open($path, $name) { return(true); } function _close() { return(true); } function _read($id) { global $database_connection; global $table_prefix; $id = mysql_real_escape_string($id); $sql = "SELECT internal_session_data FROM " . $table_prefix . "sessions WHERE id = '$id'"; if ($result = mysql_query($sql, $database_connection)) { if (mysql_num_rows($result)) { $record = mysql_fetch_assoc($result); return base64_decode($record['internal_session_data']); } } return ''; } function _write($id, $data) { if ($data == "") { return true; } global $database_connection; global $table_prefix; $clean_data = base64_encode(serialize($_SESSION)); $access = time(); $id = mysql_real_escape_string($id); $access = mysql_real_escape_string($access); $data = base64_encode($data); $sql = "REPLACE INTO " . $table_prefix . "sessions VALUES ('$id', '$access', '$data', '$clean_data')"; return mysql_query($sql, $database_connection); } function _destroy($id) { global $database_connection; global $table_prefix; $id = mysql_real_escape_string($id); $sql = "DELETE FROM " . $table_prefix . "sessions WHERE id = '$id'"; return mysql_query($sql, $database_connection); } function _clean($max) { global $database_connection; global $table_prefix; $old = time() - $max; $old = mysql_real_escape_string($old); $sql = "DELETE FROM " . $table_prefix . "sessions WHERE php_time < '$old'"; return mysql_query($sql, $database_connection); } ---- Server IP: 69.147.83.197 Probable Submitter: 142.68.109.126 ---- Manual Page -- http://www.php.net/manual/en/function.session-set-save-handler.php Edit -- https://master.php.net/note/edit/105704 Del: integrated -- https://master.php.net/note/delete/105704/integrated Del: useless -- https://master.php.net/note/delete/105704/useless Del: bad code -- https://master.php.net/note/delete/105704/bad+code Del: spam -- https://master.php.net/note/delete/105704/spam Del: non-english -- https://master.php.net/note/delete/105704/non-english Del: in docs -- https://master.php.net/note/delete/105704/in+docs Del: other reasons-- https://master.php.net/note/delete/105704 Reject -- https://master.php.net/note/reject/105704 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#183028) next »