php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #26099 session is not available in __destruct()
Submitted: 2003-11-03 15:53 UTC Modified: 2013-06-27 08:53 UTC
Votes:7
Avg. Score:3.9 ± 1.8
Reproduced:3 of 6 (50.0%)
Same Version:0 (0.0%)
Same OS:1 (33.3%)
From: nospam0 at malkusch dot de Assigned: yohgaki (profile)
Status: Closed Package: *General Issues
PHP Version: 5.0.0b2 (beta2) OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: nospam0 at malkusch dot de
New email:
PHP Version: OS:

 

 [2003-11-03 15:53 UTC] nospam0 at malkusch dot de
Description:
------------
Actually I'm talking about beta2 of PHP5, but it wasn't listed.

I think the __destruct() method should do things like cleaning up any resources. So it would be nice if I could destroy Sessions which are empty. But the Session Handler for closing the Session is called before the __destruct() methods.

Reproduce code:
---------------
class Bug {

    private $i = 0;

    public function __construct() {
        session_start();
        if ( isset( $_SESSION['i'] ) ) {
            $this->i = $_SESSION['i'];
        }
        $this->i ++;
        $_SESSION['i'] = $this->i;
    }

    public function printLink() {
        echo '<a href="/?' . SID . '">Bug</a>: ' . $this->i;
    }

    public function __destruct() {
        if ( $this->i > 4 ) {
            session_destroy();
        }
    }

}

$bug = new Bug();
$bug->printLink();

Expected result:
----------------
I would expect that the Session is still active and can be manipulated in any __destruct() method.

Actual result:
--------------
PHP's Session Handler for closing the Session is called before any __destruct method. So I can't do anything with the Session in the __destruct method.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-11-05 14:42 UTC] lokrain at gmail dot com
I think you can still call session_destroy() or any other session functions using register_shutdown_function(). You even can work around that with hidden iframes, if you want to destroy a class before the main php end, but I think this is not the case you are looking for.
 [2013-06-27 08:53 UTC] yohgaki@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: yohgaki
 [2013-06-27 08:53 UTC] yohgaki@php.net
I verified with following script. Session module is PHP_SESSION_NONE status 
after session_destroy().


<?php

class Bug {

    private $i = 0;

    public function __construct() {
        session_start();
        if ( isset( $_SESSION['i'] ) ) {
            $this->i = $_SESSION['i'];
        }
        $this->i ++;
        $_SESSION['i'] = $this->i;
    }

    public function printLink() {
        echo '<a href="/?' . SID . '">Bug</a>: ' . $this->i . PHP_EOL;
    }

    public function __destruct() {
		session_destroy();
		echo 'destruct'.PHP_EOL;
    }

}

$bug1 = new Bug();
$bug2 = new Bug();
$bug1->printLink();

unset($bug1);

var_dump(session_status());
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 17 22:01:27 2024 UTC