Programming + Design

Migrating PHP4 custom session handlers to PHP5 PDF Print E-mail
Written by Brett Brewer   
Sunday, 11 November 2007

Are you using PEAR MDB2 or PEAR DB with PHP5 and custom session handlers? I encountered a frustrating little problem today when trying to migrate a PHP4 application I wrote to PHP5. The app uses custom session handlers to save session data to a MySQL database. Unfortunately, after much hair pulling and messing around with using session_write_close() on various pages in my app to force the session data to be saved, I discovered that the problem was with the PEAR MDB2 database class. As part of my application upgrade I switched from using the deprecated PEAR DB class and moved to the PEAR MDB2 class. Unfortunately, in PHP5, the instance of my MDB2 class is destroyed prior to calling the "session_write" function, causing all session data to be lost when doing page redirects. I tried calling session_write_close() as a shutdown function using register_shutdown_function and it appeared to work. Then I did some more searching and discovered that someone else had already recommended this in the comments on the PHP web site. I don't know if this is really the best solution or whether MDB2 or PHP have a bug that needs to be addressed, but registering session_write_close() as a shutdown function appears to be the only solution that works transparently. So, if you are using PEAR MDB2 or PEAR DB and you find that your custom session handlers break when you move from PHP4 to PHP5, you can probably fix it by adding the following line to one of you global configuration files, or, better yet, just add it to whatever include file contains your custom session handlers:


Hope this saves someone else a few hours of debugging.  

Last Updated ( Sunday, 11 November 2007 )
< Prev   Next >


Who's Online

We have 1 guest online

© 2017
Joomla! is Free Software released under the GNU/GPL License.