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

From: Date: Wed, 26 Nov 2008 13:07:25 +0000
Subject: note 87245 added to function.session-set-save-handler
Groups: php.notes 
Request: Send a blank email to php-notes+get-146981@lists.php.net to get a copy of this message
Here's a few useful tips when working with custom session handlers (and sessions in general - with PHP 5.2.x)... 1) Setting session_set_save_handler() automatically sets the ini setting session.save_handler to 'user' for you - no need to do it yourself. 2) You may in fact not need to set up a __destruct() or use register_shutdown_function() to write your session data if using classes/objects. My testing with 5.2.x shows that my session data is safely saved without the need of either. I am unsure if PHP internals have changed to "fix" this and documents just aren't updated with that info, or if perhaps I haven't yet hit a scenario that would trigger this problem. 3) It's stated a few places - but just to re-iterate: you cannot serialize internal PHP objects. This means if you have a class that extends an internal class (for example, a class that extends PDO for db functions), you will not be able to save an instance of your class in a session variable. Worst off, you will get a very vague error message about it, saying an unknown exception was thrown and it won't know from where. The solution is for any class that has to be serialized, don't extend internal classes, instead have them set up an instance of that object internally via __construct() or something, and then use __sleep() and __wakeup() hooks to export/reconfigure your class variables/resources. 4) It appears that since PHP 4.x or somewhere, the seralization of data that takes place for sessions changed. It used to export human readable text much like serialize() does, however it appears that now the session data is further encoded - quite possibly to hinder session data tampering on the local filesystem. Just mentioning this if you used to view session data to see what was being saved. ---- Server IP: 69.147.83.197 Probable Submitter: 99.241.132.158 ---- Manual Page -- http://www.php.net/manual/en/function.session-set-save-handler.php Edit -- https://master.php.net/note/edit/87245 Del: integrated -- https://master.php.net/note/delete/87245/integrated Del: useless -- https://master.php.net/note/delete/87245/useless Del: bad code -- https://master.php.net/note/delete/87245/bad+code Del: spam -- https://master.php.net/note/delete/87245/spam Del: non-english -- https://master.php.net/note/delete/87245/non-english Del: in docs -- https://master.php.net/note/delete/87245/in+docs Del: other reasons-- https://master.php.net/note/delete/87245 Reject -- https://master.php.net/note/reject/87245 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#146981) next »