Stange behaviour: PHP5.2.4 userdefined Sessionhandler in write function

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Hi group,

I found something strange in PHP5.2.4 (on IIS7/Vista).

I am working on an app that has been running just fine under heavy load  
for over a year at some custumer of mine. (they have PHP5.1 on W2003  
Server, ISAPI)

I imported the whole project locally on a slightly more modern version  
of PHP 5.2.4 to make a few adjustments.

I needed a custum sessionhandler (via database) in this particular project.

So I have:

session_set_save_handler (

First thing I found locally was that my sessions didn't work.

After some errorlogging (which is not easy in sessionhandlers since you  
cannot output via echo to the client, you need to debug via a file), I  
found the reason for my trouble was the fact that I lost my $connection  

This object is a ADODB object (
I used this particular databaseabstraction layer for years without problems.

Here is the start of my modified session write handler function:

function MySession_write($ses_id, $data) {
   global $connection;

   $connExists = (isset($connection) ? "exists": "doesn't exist");

   // Hack for my version of PHP that somehow destroys connection
   if (!isset($connection)){
      require ('dbconnect.php');    


.... actual code ...

I was surprised to find in C:\inetpub\php5errlog.txt:
in MySession_write:$connExists=doesn't exist
$data=userid|i:1;realname|s:18:"   CS-Erwin Möller";isadmin|s:1:"Y";

Meaning my $connection was lost somewhere.
I use that same approach (via global $connection) in all other  
sessionhandler functions. No problems.
But in the writeroutine it is gone.

This for sure DIDN'T happen on the earlier version of PHP5 at my client.

Thus I ended up adding this stinking piece of code, to re-include the  
whole databaseconnection-code:

   // Hack for my version of PHP that somehow destroys connection
   if (!isset($connection)){
      require ('dbconnect.php');    

Any ideas what is causing this?

I was thinking that PHP was maybe clearing up its connections to the  
database first, before calling the session_write routine? Thus  
invalidating my $connection? (I am guessing here).

Did anybody see something similar?
Any clues?

Erwin Moller

Site Timeline