Session is recreated automatically, with a field inside

Asked

Viewed 76 times

0

I have a Session class where every time I urge her she automatically starts the session.

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 automatically without me passing any parameter.

And the one that is created automatically has an input called sidebar worthwhile 1 that I do not know where they come from, for at no time in my code have I declared 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');
    }
  • 2

    About you not having set this index I doubt. Do a search in your source code that will surely appear. Now, by chance, you don’t instate that class in the index php. also?

  • Yes instancio no index.php por isso é que depois que eu dou doLogout é que ele volta a classe sessão.

  • Bruno Augusto, thanks for the tip, I went to my source code, alias I had already gone but I had forgotten a folder where I had some includes and there was the error. beginner vacillation. Thank you ;)

  • Solved, then?

  • Yes, solved, thanks for the tip ;)

1 answer

2

Just so the topic doesn’t go unanswered:

If there is an entry in $_SESSION, it is because a session variable was created at some point in the code. If you don’t remember, search all your sources. Some editors or even entire Ides already have this type of resource.

Please note that your class does not complete recording of session data. The PHP session engine is lazy and only records 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 write, invoke session_write_close() at some point in your code but before that a redirect occurs, either manually or in a class method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.