php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29032 Session are closed before all object destructors executed.
Submitted: 2004-07-06 16:58 UTC Modified: 2005-03-14 01:00 UTC
Votes:18
Avg. Score:4.8 ± 0.5
Reproduced:16 of 18 (88.9%)
Same Version:12 (75.0%)
Same OS:14 (87.5%)
From: antonr at game dot permonline dot ru Assigned:
Status: No Feedback Package: Session related
PHP Version: 5CVS-2004-07-06 (dev) OS: Irrelevant
Private report: No CVE-ID: None
 [2004-07-06 16:58 UTC] antonr at game dot permonline dot ru
Description:
------------
Session are closed before all object destructors executed. It seems to be wrong, cause objects desctructors may save information in opened session.


Reproduce code:
---------------
<?php

  class MySession
    { public $number; 
      public function __construct()
        { session_start();
          $this->number = @$_SESSION["number"];
        }
      public function Process()
        { $this->number++;
        }
      public function __destruct()
        { $_SESSION["number"] = $this->number;
          session_write_close();
        }
    }

  $ss = new MySession();
  $ss->Process();
  echo $ss->number;

?>


Expected result:
----------------
With every page reloading number in the page must increase by 1.

Actual result:
--------------
Everytime number is equal 1.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-26 02:40 UTC] auroraeosrose at hotmail dot com
Modifying $_SESSION in the destructor ONLY works if you call unset($object) before the end of your script, however this is a pain in the rear - and if you have references to that object elsewhere it doesn't work properly.

There is a workaround... You CAN use register_shutdown_function in the constructor of your class

      public function __construct()
        { 
register_shutdown_function(array($this, '__destruct');
        } 

and then the destruct is called early enough that you can modify $_SESSION. But this defeats the whole purpose of a destructor - I could do that in every class if php5 didn't support destructors (in fact I did in php4) but I thought the point was to eliminate messy workarounds.  Now why in the world the sessions are closed in between when shutdown functions and destructors are called is beyond me.
 [2004-12-06 05:34 UTC] anthony at ectrolinux dot com
For reference, this bug appears to be synonymous to bug #30578, with the same cause but a different effect.
 [2005-03-06 20:35 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-03-14 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 16:01:31 2024 UTC