I'm new to PHP programming, I have code for user Login, and already connected to localhost database.
Here is the Login.php code
<html>
<head>
<title>Login Page</title>
</head>
<body>
<div>
<form action="doLogin.php" method="POST">
<p>
<label>Username : </label>
<input type="text" id="username" name="user">
</p>
<p>
<label>Password : </label>
<input type="text" id="password" name="pass">
</p>
<p>
<input type="submit" id="btnLogin" value="Login">
</p>
</form>
</div>
</body>
</html>
and doLogin.php
<?php
$username = $_POST["user"];
$password = $_POST["pass"];
$username = stripcslashes($username);
$password = stripcslashes($password);
$con = mysqli_connect('localhost', 'root', '', 'dbtest');
$query = mysqli_query($con, "select * from user where Username = '$username' and Password = '$password' ");
$row = mysqli_fetch_array($query);
if ($row['Username'] == $username && $row['Password'] == $password)
{
echo "You are Success Login!!! Welcome ".$row['Username'];
header('Location: Profile.php');
}
else
{
echo "Failed to login!";
}
?>
I want to using session
so if the user not logged in, it will redirect to Login page. Where and how I apply a session into my code, much appreciate it.