php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Thanks for voting! Your vote should be reflected in the statistics below.
Doc Bug #33772 __destruct is run too early (session_save_handler)
Submitted: 2005-07-19 16:47 UTC Modified: 2005-09-20 15:17 UTC
Votes:22
Avg. Score:4.8 ± 0.6
Reproduced:22 of 22 (100.0%)
Same Version:10 (45.5%)
Same OS:10 (45.5%)
From: msipria at suomi24 dot fi Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5CVS-2005-07-19 OS: *
Private report: No CVE-ID: None
 [2005-07-19 16:47 UTC] msipria at suomi24 dot fi
Description:
------------
I have session handling class and i have db class that i pass to the session handling class.

As of PHP 5.1.0b1 order of going throu __destructers have been changed, so at end eaven DB class exsist its __destucter has been runned.

removing __destruct from DB class will not help, coz mysql link has own __destruct function that it will run.

i realy hope this is bug (that can fix) and it will be fixed, other whay i'm stuck with 5.0.x or have to re write lots of source code.


Reproduce code:
---------------
http://www.wiofso.com/~msipria/testi.txt

Expected result:
----------------
In PHP 5.0.x (working)

__construct = LINK
open = LINK
read = LINK
write = LINK
close = LINK
__destruct = KILLED LINK

Actual result:
--------------
In PHP 5.1.0b3 (tested b1-b3) (not working)

__construct = LINK
open = LINK
read = LINK
__destruct = KILLED LINK
write = KILLED LINK
close = KILLED LINK

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-19 21:20 UTC] sniper@php.net
I need this fixed too, it's not possible to use e.g. mysqli as save handler otherwise..

 [2005-07-21 00:10 UTC] toma at smartsemantics dot com
Adding session_write_close() to the __destruct() function will restore the write, close, and destroy functions to their proper order.

I use a custom session handler that extends my custom mysqli class.  It automatically checks for lost database connections and attempts reconnects, etc.

This is still a bug, but the following makes your custom session handler viable.

public function __destruct(){
	@session_write_close();
}

Without session_write_close:
starting session
connecting to database
destroying session
writing session
can't write, database connection does not exist
connecting to database
writing session
closing session

With session_write_close:
starting session
connecting to database
writing session
closing session
destroying session
 [2005-07-21 16:38 UTC] msipria at suomi24 dot fi
my class isn't extension of mysqli class, it just use it as object, and with real class i have tryed session_write_close() function but mysql connection is deat, i don't wanna restor it, i just wanna that it will be alive at the end like now.
 [2005-08-09 14:01 UTC] dmitry@php.net
This is chicken/egg problem and it cannot be solved. One expect that destructors are called before session close the other uses object for save handlers.
 [2005-08-09 14:55 UTC] sniper@php.net
This patch fixes this problem by doing php_session_flush() before any __destruct()'s are called:

  http://www.php.net/~jani/patches/bug33772.patch

Note: It won't work if you have compiled ext/session as shared. We need a better solution, I'm submitting this just  as as temporary one.

 [2005-08-23 03:03 UTC] sniper@php.net
Reclassified as documentation issue.

 [2005-08-24 23:58 UTC] webmaster at vintagegaming dot org
I want to enter this as a new bug report but I expect it would be kicked out as a duplicate of 33635, which links to this bug.  This seems related in that something has changed so that my mysqli link is closed before the shutdown_function is executed.

The following code works in all php5 versions before 5.1beta3.

<?php

function shutdown()
{
	global $link;

	echo mysqli_real_escape_string($link, 'foo');
}

$servername = 'localhost';
$username = 'username';
$password = 'password';

$link = mysqli_init();
mysqli_real_connect($link, $servername, $username, $password);

echo mysqli_real_escape_string($link, 'foo') . '<br />';

register_shutdown_function('shutdown');

?>

Expected Result:
foo
foo

Actual Result:
foo
Warning: Couldn't fetch mysqli in c:\program files\apache group\Apache\htdocs\foo.php on line 7

and to further this annoying issue, why is it that the equivalent mysql function still works in 5.1b3?

<?php

function shutdown()
{
	global $link;
	
	echo mysql_real_escape_string('foo', $link);
}

$servername = 'localhost';
$username = 'username';
$password = 'password';

$link = mysql_connect($servername, $username, $password);

echo mysql_real_escape_string('foo', $link) . '<br />';

register_shutdown_function('shutdown');

?>

Expected Result:
foo
foo

Actual Result:
foo
foo
 [2005-09-09 21:56 UTC] tss24 at cornell dot edu
Experiencing the same exact problem with PHP 5.0.5 and MySQL 4.1.11

Was this a new bug with PHP 5.0.5 / 5.1.x?
 [2005-09-09 22:01 UTC] earnest dot berry at gmail dot com
I also am having the issue. I'm using 5.1.0b3 with mysql 4.1.13a-nt. Will this be fixed in the next relase of PHP?
 [2005-09-10 12:34 UTC] zulan at zulan dot net
If this is the egg, the chicken is: http://bugs.php.net/bug.php?id=27555

Is there a way to make it ini_set-able if objects->__destruct or sess_write_close first?
 [2005-09-20 15:17 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"Write and Close handlers are called after destructing objects since PHP 5.0.5. Thus destructors can use sessions but session handler can't use objects. In prior versions, they were called in the opposite order. It is possible to call session_write_close() from the destructor to solve this chicken and egg problem."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 12:01:29 2024 UTC