<?php
session_start(); 

if (isset($_SESSION["Logdin"]) && $_SESSION["Logdin"] === true && $_SESSION["admin"]===true) {
    header("location:./index.php");
    exit;
}

require_once "./connection.php";

$username = trim($_POST["username"]);
$userpass = md5(trim($_POST["userpass"]));

$q = "SELECT ID, username, userpass, user_type FROM userstbl WHERE username = ? AND userpass = ?";
$stmt = $conn->prepare($q);
$stmt->bind_param("ss", $username, $userpass);
$stmt->execute();
$result1 = $stmt->get_result();

if ($result1 && $result1->num_rows > 0) {
    $row11 = $result1->fetch_assoc();
    $_SESSION["user_id"] = $row11["ID"];
    $_SESSION["user_name"] = $row11["username"];
    $_SESSION["user_type"] = $row11["user_type"]; 
    if($row11["user_type"]=="admin")
    {
        $_SESSION["admin"]=true;
    }
    // Added this line
    $_SESSION["Logdin"] = true;
    echo '<script> window.location.assign("./index.php"); </script>';
} else {    
    echo '<script>alert("The given information is not matching with any account."); window.location.assign("./login.php"); </script>';
}

$stmt->close();
$conn->close();
?>
