brettbrewer.com

Programming + Design

Home arrow News arrow PHP arrow Migrating PHP4 custom session handlers to PHP5
Migrating PHP4 custom session handlers to PHP5 PDF Print E-mail
Written by Administrator   
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:

register_shutdown_function('session_write_close');

Hope this saves someone else a few hours of debugging.  

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

Search


Brett highly recommends...

Get Your War On: The Definitive Account of the War On Terror 2001-2008

My favorite comic is getting ready to be relased as a single volume. You can preorder now. If you hate the war and love to laugh, do yourself a favor and order this. You can also read all the GYWO comics online for free here.

BBS The Documentary

This is by far the most comprehensive and entertaining documentary I have ever seen regarding the early BBS system. If you're serious about knowing your internet history, or you just want some extra geek-cred, this is your new favorite documentary.

The Nada Chair "BackUp"

The best alternative I have found to overpriced ergonomic office chairs, especially for very low pain around the sacral joint. Works better than my Aeron chair ever did. Also, it comes in navy blue and black, in case you're not into the whole pregnant woman aesthetic (pictured).It's really not meant for sitting on the floor, it's really better for seats that offer poor back support, and works great at sporting events to make sitting in bleachers much more comfortable.

Antec Quiet PC Case

At just under $120 including a 500 Watt power supply, this "quiet pc" case has worked great for me for over 2 years. In fact I now own two great Antec cases/PSUs. For the money, it's hard to beat this combination.

Valid XHTML 1.0 Transitional

© 2008 www.brettbrewer.com

Spam bots check out this page.

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