Re: register_session_gc_handler proposal

From: Date: Tue, 15 May 2007 18:47:37 +0000
Subject: Re: register_session_gc_handler proposal
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-29449@lists.php.net to get a copy of this message
Emil Ivanov wrote:
The case is simple: A user is allowed to upload files to the server, which are store in a temporary location and their filenames are stored in the session. The user leaves the site and the session gets destroyed, but the files remain untouched. If I can hook to the gc session function that will solve my case, but currently with session_set_save_handler I have to rewrite session handling, which I don't need... So a simple hook will be very useful. Hi,
I've also been looking into how this could be implemented elegantly and I'd like to propose the following: - Create a class exposed to userland called something like SessionDefaultHandler, which wraps the session module defined at startup. - Alter session_set_save_handler() to alternatively take a single argument, a class name. - The given handler class must extend SessionDefaultHandler and may override any of its methods. - Alter the user session module to use the child class if it's been registered. This would provide an elegant solution to your particular problem, as well as allowing any default session functions to be overridden, wrapped or called independently. <?php class MySessionHandler extends SessionDefaultHandler { function gc($maxlifetime) {
       parent::gc($maxlifetime);
       $time = time();
       foreach (glob('images/*') as $file) {
           if ($time - filemtime($file) > $maxlifetime) {
               unlink($file);
           }
       }
       return true;
} } session_set_save_handler('MySessionHandler'); ?> Any thoughts? Arpad

« previous php.internals (#29449) next »