I have a login page in PHP
just done for testing, I know my code is not secure.
I did below code to get some data from table and display in table:
session_start();
include("config.php");
if(isset($_SESSION['email']) && $_SESSION['email'] == true){
$user_email=$_SESSION['email'];
$check_user="select * from admin WHERE user_email='$user_email'";
$run=mysqli_query($link,$check_user);
while($row = $run->fetch_assoc()){
$_SESSION['access']=$row['access'];
$_SESSION['name']=$row['user_name'];
}
}
Till here it's working properly, when I login into my page using following code for login page:
session_start();
include("config.php");
if(isset($_POST['login'])){
$user_email=$_POST['email'];
$user_pass=$_POST['pass'];
$check_user="select * from admin WHERE user_email='$user_email'AND user_pass='$user_pass'";
$run=mysqli_query($link,$check_user);
if(mysqli_num_rows($run)>0){
$_SESSION['email']=$user_email;
$_SESSION['access']=$result['access'];
//here session is used and value of $user_email store in $_SESSION.
echo "<script>window.open('index.php','_self')</script>";
}else{
echo "<script>alert('Email or password is incorrect!')</script>";
}
}
But when I'm trying to add the following code for redirecting user if not logged in, even if I login I'm redirected to login page again and again.
The mistake is with the below code:
session_start();
include("config.php");
if(isset($_SESSION['email']) && $_SESSION['email'] == true){
$user_email=$_SESSION['email'];
$check_user="select * from admin WHERE user_email='$user_email'";
$run=mysqli_query($link,$check_user);
while($row = $run->fetch_assoc()){
$_SESSION['access']=$row['access'];
$_SESSION['name']=$row['user_name'];
}
}
//if login in session is not set
if(!isset($_SESSION['login'])){
header("Location: login.php");
}
I am new to php, can anyone please tell me what is wrong with my code?