note 37995 modified in function.session-set-save-handler by danbrown

From: Date: Sun, 23 Jan 2011 16:21:06 +0000
Subject: note 37995 modified in function.session-set-save-handler by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176054@lists.php.net to get a copy of this message
I found the need to have a garbage collection function run with my sessions so I had to write my own session handler. I took the example code above but found that it had a problem, it does not open the session file exclusively. This means that if you have a frameset where 2 pages are accessing the session at the same time and both modify the session only the last page to finish processing will have its session data saved. The first page will have all of its session data overwritten. So here is a modified version of a file session handler in PHP that has file locking. (Not supported on FAT or NFS apparently) <?php $sess_save_path = $sess_session_name = $fp=null; function open ($save_path, $session_name) { global $sess_save_path, $sess_session_name; $sess_save_path = $save_path; $sess_session_name = $session_name; return(true); } function close() { global $fp; flock($fp, LOCK_UN); fclose($fp); $fp=null; return(true); } function read ($id) { global $sess_save_path, $sess_session_name, $fp; $sess_file = "$sess_save_path/sess_$id"; if ($fp = @fopen($sess_file, "r+")) { flock($fp, LOCK_EX); $sess_data = fread($fp, filesize($sess_file)); return($sess_data); } else { return(""); // Must return "" here. } } function write ($id, $sess_data) { global $sess_save_path, $sess_session_name, $fp; $sess_file = "$sess_save_path/sess_$id"; if (!empty($fp)) { fseek($fp,0); return(fwrite($fp, $sess_data)); } elseif ($fp = @fopen($sess_file, "w")) { flock($fp, LOCK_EX); return(fwrite($fp, $sess_data)); } else { return(false); } } function destroy ($id) { global $sess_save_path, $sess_session_name; $sess_file = "$sess_save_path/sess_$id"; return(@unlink($sess_file)); } function gc ($maxlifetime) { global $sess_save_path, $sess_session_name; $sess_file = "$sess_save_path/sess_$id"; return true; } session_set_save_handler ("open", "close", "read", "write", "destroy", "gc"); ?> enjoy --was-- I found the need to have a garbage collection function run with my sessions so I had to write my own session handler. I took the example code above but found that it had a problem, it does not open the session file exclusively. This means that if you have a frameset where 2 pages are accessing the session at the same time and both modify the session only the last page to finish processing will have its session data saved. The first page will have all of its session data overwritten. So here is a modified version of a file session handler in PHP that has file locking. (Not supported on FAT or NFS apparently) $sess_save_path = $sess_session_name = $fp=null; function open ($save_path, $session_name) { global $sess_save_path, $sess_session_name; $sess_save_path = $save_path; $sess_session_name = $session_name; return(true); } function close() { global $fp; flock($fp, LOCK_UN); fclose($fp); $fp=null; return(true); } function read ($id) { global $sess_save_path, $sess_session_name, $fp; $sess_file = "$sess_save_path/sess_$id"; if ($fp = @fopen($sess_file, "r+")) { flock($fp, LOCK_EX); $sess_data = fread($fp, filesize($sess_file)); return($sess_data); } else { return(""); // Must return "" here. } } function write ($id, $sess_data) { global $sess_save_path, $sess_session_name, $fp; $sess_file = "$sess_save_path/sess_$id"; if (!empty($fp)) { fseek($fp,0); return(fwrite($fp, $sess_data)); } elseif ($fp = @fopen($sess_file, "w")) { flock($fp, LOCK_EX); return(fwrite($fp, $sess_data)); } else { return(false); } } function destroy ($id) { global $sess_save_path, $sess_session_name; $sess_file = "$sess_save_path/sess_$id"; return(@unlink($sess_file)); } function gc ($maxlifetime) { global $sess_save_path, $sess_session_name; $sess_file = "$sess_save_path/sess_$id"; return true; } session_set_save_handler ("open", "close", "read", "write", "destroy", "gc"); enjoy http://php.net/manual/en/function.session-set-save-handler.php

« previous php.notes (#176054) next »