I'm setting a web application for an E-commerce project which has an administrator panel. How do I force and auto redirect to login.php instead of index.php?
For instance, if I did follow this path http://localhost/project/admin (this will load index.php).
However, the login process runs successfully and session opened when I try to open login.php manually via http://localhost/project/admin/login.php
Folder Structure:
- Both index.php and login.php are in admin folder
- connection.php, admin-header, admin-footer are in include folder Path http://localhost/project/admin
using XAMPP Panel v3.2 on windows 10
I've tried the below:
login.php
<?php
session_start();
include '../include/connection.php';
if (isset($_POST['submit'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$query = "select * from admin where admin_email = '$email' AND admin_password = '$password'";
$result = mysqli_query($conn,$query);
$adminSet = mysqli_fetch_assoc($result);
if ($adminSet['admin_id']) {
$_SESSION['admin_id'] = $adminSet['admin_id'];
header("location:index.php");
}
else {
$error = "You are not authenticated.";
}
}
?>
admin-header.php // which included on index.php
<?php
session_start();
if (!isset($_SESSION['admin_id'])) {
header("location:login.php");
}
?>
index.php
<?php include '../include/admin-header.php'?>