In my website I created two separate homepages one for admin another for users When I login as admin it is redirecting to home.php but when I visit website using it's address (already logged in as admin) index.php opens how can I redirect admin automatically to home.php. Here is a relevant code login function in functions.php file

function login(){
global $db, $username, $errors;

// grap form values
$username = e($_POST['username']);
$password = e($_POST['password']);

// make sure form is filled properly
if (empty($username)) {
    array_push($errors, "Username is required");
if (empty($password)) {
    array_push($errors, "Password is required");

// attempt login if no errors on form
if (count($errors) == 0) {
    $password = md5($password);

    $query = "SELECT * FROM users WHERE username='$username' AND password='$password' LIMIT 1";
    $results = mysqli_query($db, $query);

    if (mysqli_num_rows($results) == 1) { // user found
        // check if user is admin or user
        $logged_in_user = mysqli_fetch_assoc($results);
        if ($logged_in_user['user_type'] == 'admin') {

            $_SESSION['user'] = $logged_in_user;
            $_SESSION['success']  = "You are now logged in";
            header('location: /admin/home.php');          
            $_SESSION['user'] = $logged_in_user;
            $_SESSION['success']  = "You are now logged in";

            header('location: /index.php');
    }else {
        array_push($errors, "Wrong username/password combination");

function isAdmin()
if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'admin' ) {
    return true;
    return false;

All important work is done in login function which checks if visitor is admin,if yes then sends to home.php, so I tried calling


From index.php but it didn't work.

0 Answers