I'm learning php and html from scratch, and I have this project in which I'd like to display the username after logging in.
I've stated that:
<a>Hello, <?php
if(isset($_SESSION['username'])){
echo $_SESSION['username'];
} else {
echo "user";
}
?>
</a>
at the html field in which I want the username to show. It worked before. However, after logging out, the username is no longer there.
This is the code for the logout:
<?php
session_start();
session_unset();
header("location:index.php");
exit();
?>
And the one for logging in:
<?php
session_start();
$user = $_POST['login'];
$pass = md5($_POST['pword']);
$entrar = $_POST['entrar'];
$conn = mysqli_connect("localhost","root", "", "pdsys", "3308");
if(isset($entrar)){
$verifica = mysqli_query($conn,"SELECT * FROM users WHERE username = '$user' AND pword = '$pass'") or die ("Usuário ou senha incorretos");
if (mysqli_num_rows($verifica)<=0){
echo "<script language='javascript' type='text/javascript'>
alert('Login e/ou senha incorretos');window.location
.href='index.php';</script>";
die();
} else {
setcookie("login", $user);
header("location: main.php");
}
}?>
I figure it might have something to do with the session_destroy(), but I can't figure it out how to make it work again. It just displays "user".
Everything else is functional = the connection with the database, the login system etc.