I'm trying to make login form that checks from database if username exist, if it is true, then it redirect me to another page that says welcome + username, I want to know if that need session to achieve the condition or not? because my code has no error and it is not showing the welcome message.
Let me explain it, i have variable in login.php called "$welcome" it's suppose to be echoed when the login form is submitted with valid username & password, then it redirect me to admin-panel.php and echo the variable "$welcome". Here's my work
login.php
<?php
session_start();
include "connection.php";
?>
<?php
if(isset($_POST['submit'])){
$admin_user = isset($_POST['username']) ? $_POST['username'] : '';
$admin_pass = isset($_POST['password']) ? $_POST['password'] : '';
$sql = mysqli_stmt_init($conn);
if ($sql = mysqli_prepare($conn, "SELECT username, password FROM account WHERE username = ? AND password = ?")){
mysqli_stmt_bind_param($sql, "ss", $admin_user, $admin_pass);
mysqli_stmt_execute($sql);
mysqli_stmt_bind_result($sql, $username,$password);
mysqli_stmt_fetch($sql);
mysqli_stmt_close($sql);
}
global $username, $password;
if(empty($admin_user) || empty($admin_pass)){
$msg = "<span class='text-danger'> Username or Password Can't be empty </span>";
} elseif($admin_user != $username || $admin_pass != $password){
$msg = "<span class='text-danger'> Username or Password is not valid </span>";
} else{
header("location:admin-panel.php");
$welcome = "<h2>Welcome ". $admin_user ."!!!</h2>";
$_SESSION['welcome'] = $welcome;
}
mysqli_close($conn);
}
?>
index.php
<?php
include "login.php";
global $msg;
?>
<!DOCTYPE html>
<html>
<head>
<title>Admin Panel</title>
</head>
<body>
<form method="post">
<?php if(isset($msg)){ ?>
<div>
<?php echo $msg; ?>
</div>
<?php } ?>
<div>
<label for="username">Username:</label>
<input type="text" name="username">
</div>
<div>
<label for="password">Password:</label>
<input type="password" name="password">
</div>
<div>
<input type="submit" name="submit">
</div>
</form>
admin-panel.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome Admin</title>
</head>
<body>
<div>
<?php
echo $_SESSION['welcome'];
?>
</div>
</body>
</html>