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.