PHP logout with cookies and sessions

0

I have a problem logging into my site, because when I use only sessions it logs out normally, but when I use sessions and cookies to remind the user, I need to go out twice, sometimes even more, type I am in the user page and click on exit, it reloads and still continues with the user, I click to leave again then yes it leaves the user.

The button that calls the logout function:

 <li><a href="javascript:void(0)" onclick="deslogar()">Desconectar</a></li>

JS function:

     function deslogar(){
    $.post('/', {
        sair:'sair'
    });
    document.location.href="/";
}

PHP function that will call function logout:

if(isset($_POST['sair'])){
        $logar = new SistemaLogin;
        $logar ->desconectar();
    }

Function logout php:

public function desconectar(){
        SistemaLogin::excluirCookies();
            session_destroy();
            header("Location: /cadastro_prof");
    }

function to delete cookies:

private function excluirCookies(){
            setcookie("email", "", time() - $this->tempo_cookie);
            setcookie('password', "", time() - $this->tempo_cookie);
            setcookie("tp_usuario", "", time() - $this->tempo_cookie);  
}

I create the sessions and cookies so if the user does not want to keep saved their login, the system only creates the sessions, otherwise it creates the sessions and cookies:

private function criarSessions($pri_nm, $email, $senha, $tp_usuario){
        $_SESSION['pri_nome'] = $pri_nm;
        $_SESSION['email'] = $email;
        $_SESSION['senha'] = $senha;
        $_SESSION['tp_usuario'] = $tp_usuario;
        $this->registrarLog();
    }

    private function criarCookies($email, $password, $tp_usuario){
         setcookie("email", $email, time()+$this->tempo_cookie, "/");
         setcookie('password', $password, time()+$this->tempo_cookie,"/");
         setcookie("tp_usuario", $tp_usuario, time()+$this->tempo_cookie, "/");
    }


    if($this->manter_online == 'sim'){
                        $this->criarCookies($this->email, $this->senha, $this->tp_usuario);
                        $this->criarSessions($this->pri_nm, $this->email, $this->senha, $this->tp_usuario);
                    }else{
                        $this->criarSessions($this->pri_nm, $this->email, $this->senha, $this->tp_usuario);
                    }

I put the term in 1-year cookies, and if it goes out it subtracts the same.

    
asked by anonymous 24.07.2015 / 16:05

2 answers

0

I found out what it was, I created a ClassLogin class, and a private deleteCookies function and another public disconnect, I was called a function from within the class as follows: SystemLogin :: deleteCookies (); I put it this way: $ this- > deleteCookies (); and it worked, sucking that right. I lost the night yesterday trying to find out what was ¬¬.

    
25.07.2015 / 02:27
0

Put the refresh of the page after AJAX returns:

function deslogar(){
    $.post('/', {
        sair:'sair'
    }, function(data){
       if (window.location.origin == undefined)
          window.location.origin = '//' + window.location.host;
       document.location.href = window.location.origin;
    }, function(x,y,z){
       alert('Não foi possível sair');
       console.log(x,y,z);
    });
}

Make sure cookies are cleaned up:

private function excluirCookies(){
    unset($_COOKIE['email']);
    unset($_COOKIE['password']);
    unset($_COOKIE['tp_usuario']);
    setcookie('email', null, -1, '/');
    setcookie('password', null, -1, '/');
    setcookie('tp_usuario', null, -1, '/');  
}

Delete Coockies

I noticed that you store the password in a cookie and this is a terrible practice, cookies can easily be stolen. Here's a question and a great answer on how to Remember User Safely .

    
24.07.2015 / 16:53