I have a login system and when the users login, they are sent to a new file called user.php
. In the login file, I have this code:
$user = $check->fetch_assoc();
if (password_verify($_POST['password'], $user['password'])) {
$_SESSION['logged_in'] = true;
$_SESSION['username'] = $user['username'];
header('location: user.php');
}
and when the user successfully logs in, he is sent to the user.php
file and the code in the the file looks like:
<?php
// Start The session
session_start();
// Chaeck if the user is logged in.
if ($_SESSION['logged_in'] = false) {
$_SESSION['message'] = 'You must Login to continue use this section.';
header('location: error.php');
} else {
$username = $_SESSION['username'];
echo $username;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dashboard</title>
</head>
<body>
<h1>
Welcome, <?php echo $username?>,
</h1>
</body>
</html>
But when the user logs in he gets the undefined index: username. I want to know why if I am using Sessions.