Logout problem with php

1

I'm trying to make a simple login system to learn and improve it, I used a basic material I saw in devmedia, and the matter was already old because it still used mysql instead of mysqli for connection, in that matter did not contain logout, after logging in, to clear only clearing cookies through the browser.

I am trying to implement a logout, but I am not getting it, after entering the logout page a message appears, however when it is redirected to the index it still appears as if logged in.

I tried several things and nothing worked, I tried setcookie, and when I went to the index page, the login appeared as a blank, but not as null.

I'm putting the index.php, login.php, and logout.php code below, besides they also have the login and html pages, and I'm pretty sure the problem is not there, because the register is saving correctly in the database, and I doubt much of that is a html error. I left the logout this way because it was what I saw most in my searches.

index.php:

<?php
    header("Content-type: text/html; charset=utf-8");

    $login_cookie = $_COOKIE['login'];

    if(isset($login_cookie) === true)
    {
        echo"Bem-Vindo, $login_cookie <br>";
        echo"Essas informações <font color='red'>PODEM</font> ser acessadas por você";
        echo"<br><a href='logout.php'>Sair</a>";
    }
    else
    {
        echo"Bem-Vindo, convidado <br>";
        echo"Essas informações <font color='red'>NÃO PODEM</font> ser acessadas por você";
        echo"<br><a href='login.html'>Faça Login</a> Para ler o conteúdo";
    }
?>

login.php:

<?php
    header("Content-type: text/html; charset=utf-8");

    $connect = mysqli_connect('localhost','root','','logindevmedia') or die('Erro ao conectar ao banco de dados');

    if (isset($_POST['login']) === true) 
    {
        $login = $_POST['login'];
    } 
    else 
    {
        $login = false;
    }

    if (isset($_POST['senha']) === true) 
    {
        $senha = MD5($_POST['senha']);
    } 
    else 
    {
        $senha = false;
    }

    if (isset($_POST['entrar']) === true) 
    {
        $entrar = $_POST['entrar'];
    } 
    else 
    {
        $entrar = false;
    }

    if (isset($entrar)) 
    {
        $query_select = "SELECT * FROM usuarios WHERE login = '$login' AND senha = '$senha'";

        $verifica = mysqli_query($connect,$query_select) or die("erro ao selecionar");

        if (mysqli_num_rows($verifica)<=0)
        {
            echo"<script language='javascript' type='text/javascript'>alert('Login e/ou senha incorretos');window.location.href='login.html';</script>";
            die();
        }
        else
        {
            setcookie("login",$login);
            header("Location:index.php");
        }
    }
?>

logout.php

<?php
    header("Content-type: text/html; charset=utf-8");

    session_start();

    session_unset();   // remove all session variables

    session_destroy();  // destroy the session

    echo "<script>alert('Você saiu!'); document.location.href='index.php';</script>";
?>
    
asked by anonymous 04.07.2018 / 22:54

1 answer

2

You are using a cookie to log in the user, however at the time of logging you are clearing session variables, so it does not work. You must remove the cookie.

    
05.07.2018 / 00:09