note 105704 deleted from function.session-set-save-handler by danbrown

From: Date: Thu, 08 Sep 2011 16:17:44 +0000
Subject: note 105704 deleted from function.session-set-save-handler by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183043@lists.php.net to get a copy of this message
Note Submitter: gerald dot benson at gmail dot com ---- 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); }

« previous php.notes (#183043) next »