1
I’m trying to make a simple login system to learn and improve it, I used base a subject I saw in devmedia, and the matter was old, because still used mysql instead of mysqli for connection, in this matter did not contain logout, after logging in, to use only by clearing cookies by the browser.
I’m trying to implement a logout, but I’m not getting it, after entering the logout page appears a message, but when it is redirected to the index still appears as if you are logged in.
I tried several things and nothing worked, I tried even setcookie, and when I went to the index page, login appeared as a blank space, but not as null.
I am putting the code of the pages index.php, login.php and logout.php below, besides they also have the registration pages and the login html, and I am almost sure that the problem is not there, because the registration is saving correctly in the bank, And I highly doubt that’s an HTML error. I left the logout this way because it was what I saw most in my research.
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>";
?>
Yes, I’m trying to remove the cookie but I’m not getting it, I’ve tried unset($_COOKIE['login']); session_unset($_COOKIE['login']); setcookie('login'); but nothing works.
– DaniloMB
Do
setcookie("login","",time()-3600)
– Felipe J. R. Vieira
if all pages are in the same directory
setcookie("login","",time()-3600)
will work– user60252
It worked, but there was a question, should I leave my logout as is, or should I remove the Sesssions and leave only the setcookie? I tested only with setcookie("login","",time()-3600) and it already depresses.
– DaniloMB
Apparently your application does not use Sessions at any time, so you can remove this snippet of code. If you use, you should leave.
– Felipe J. R. Vieira