Session is recreated automatically, with a field inside [closed]

0

I have a class of Sessions in which every time I instantiate it, it automatically logs in.

When in another file I do I destroy the session through a method called doLogout I see that it automatically deletes my session as expected but creates another one automatically without passing any parameters.

And the one that is created automatically has an entry named sidebar of value 1 that I do not know where they come from, because at no point in my code did I declare it.

Class

class sessao{
   protected $id_ur;
   protected $nvars;

public function __construct($inicia=true){
    if($inicia==TRUE){
        $this->start();
    }
}
public function start(){
    session_start();
    $this->id_ur = session_id();
    $this->setNvars();
}
private function setNvars(){
    $this->nvars = sizeof($_SESSION);
}
public function getNvars(){
    return $this->nvars;
}
public function setVar($var, $valor){
    $_SESSION[$var] = $valor;
    $this->setNvars();
}
public function unsetVar($var){
    unset($_SESSION[$var]);
    $this->setNvars();
}
public function getVar($var){
    if(isset($_SESSION[$var])){
        return $_SESSION[$var];
    }else{
        return NULL;
    }
}
public function destroy($inicia=false){
    session_unset();
    session_destroy();
    $this->setNvars();
    if($inicia==TRUE){
        $this->start();
    }

}
public function printAll(){
    foreach ($_SESSION as $k => $v){
        printf("%s = %s<br />", $k, $v);
    }
}
}
?>

Function doLogout() mentioned

public function doLogout(){
            $sessao = new sessao();
            $sessao->destroy(TRUE);
            redireciona('index.php');
    }
    
asked by anonymous 27.11.2014 / 13:49

1 answer

2

Just so that the topic is not left without a proper response:

If an entry exists in $ _SESSION, it is because a session variable was created at some point in the code. If you do not remember, do a search on all your source codes. Some publishers or even entire IDEs already have this resource type feature.

Note that your class does not end the recording of session data. The PHP session engine is lazy and only writes the additions made in the "last second."

If a redirect is done before PHP "reacts" and writes the data, you will lose the data.

To force data to be written, invoke session_write_close () at some point in your code but before > that a redirection occurs, either manually or in a class method.

    
30.11.2014 / 02:11