0

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.

  • PS: I mentioned session_destroy(); because it was what I used earlier. I changed it to session_unset() in hopes it would display the username after login, but it didn't. – André Braga May 31 '20 at 05:30
  • 1
    **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman May 31 '20 at 13:28

2 Answers2

0

Worked it out by adding a line to set the value of session username:

<?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);
        $_SESSION['username']=$user;
        header("location: main.php");
    }
}?>
0

Use

session_destroy();

in logout functionality.