My login page is refusing to redirect to the home page but when the session verification code is taken off from the home page, it works. I am trying to ensure no user can access the home page without logging Please help me out . Thanks
Index.php
<!DOCTYPE html><?php session_start();?>
<html>
<head>
<title>User Login</title>
</head>
<body>
<form action="login.php" method="post">
<table width="500" align="center" bgcolor="skyblue">
<tr align="center">
<td colspan="3"><h2>User Login</h2></td>
</tr>
<tr>
<td align="right"><b>Email</b></td>
<td><input type="text" name="email" required="required"/></td>
</tr>
<tr>
<td align="right"><b>Password:</b></td>
<td><input type="password" name="pass" required="required"></td>
</tr>
<tr align="center">
<td colspan="3">
<input type="submit" name="login" value="login"/>
</td>
</tr>
</table>
</form></body></html>
Login.php
<?php
$con = mysqli_connect("localhost","root","","test");
if (mysqli_connect_errno())
{
echo "MySQLi Connection was not established: " . mysqli_connect_error();
}
if(isset($_POST['login'])){
$email = mysqli_real_escape_string($con,$_POST['email']);
$pass = mysqli_real_escape_string($con,$_POST['pass']);
$sel_user = "select * from users where user_email='$email' AND user_pass='$pass'";
$run_user = mysqli_query($con, $sel_user);
if (mysqli_num_rows($run_user)>0)
{
$_SESSION['user_email']=$email;
header('Location: home.php');
}
else {
echo "<script>alert(' Email or password is not correct, try again!')</script>";
}
}
?>
Home.php
<?php
session_start();
if (!isset($_SESSION['nID']))
header("Location: index.php");
?>
<html>
welcome
</html>
session.php
<?php
$con = mysqli_connect("localhost","root","","tech_dept");
if (mysqli_connect_errno())
{
echo "MySQLi Connection was not established: " . mysqli_connect_error();
}
$user_check=$_SESSION['leaders_username'];
$sel_user = "Select leaders_username from login where leaders_username='$user_check'";
$ses_sql = mysqli_query($con, $sel_user);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['leaders_username'];
if(!isset($login_session)){
mysql_close($con);
header('Location: index.php');
}
?>