Stange behaviour: PHP5.2.4 userdefined Sessionhandler in write function

Stange behaviour: PHP5.2.4 userdefined Sessionhandler in write function

am 19.09.2007 18:34:13 von Erwin Moller

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 (
'MySession_open',
'MySession_close',
'MySession_read',
'MySession_write',
'MySession_destroy',
'MySession_gc');

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
object.

This object is a ADODB object (www.phplens.com/adodb).
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');
}

error_log("\ninMySession_write:\$connExists=".$connExists."\ n\$ses_id=$ses_id\n\$data=$data\n\n",3,'C:\inetpub\php5errlo g.txt');

..... actual code ...
}

I was surprised to find in C:\inetpub\php5errlog.txt:
in MySession_write:$connExists=doesn't exist
$ses_id=d86415c53f9c9f4125cff6b2725eb61e
$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?

Regards,
Erwin Moller