<?php
ini_set('session.save_path', __DIR__ . '/sessions');
ini_set('session.use_only_cookies', 1);
ini_set('session.use_strict_mode', 1);
session_set_cookie_params([
    'lifetime' => 0,
    'path' => '/',
    'domain' => '',   // IMPORTANT
    'secure' => false, // true if HTTPS
    'httponly' => true,
    'samesite' => 'Lax'
]);

session_start();
include("connection.php");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "SELECT * FROM admin where username = '$username'";
  
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);

    if ($row) {
        if ($username == $row['username'] && $password == $row['password']) {

            $_SESSION['user_name'] = $username;

        // IMPORTANT
        session_regenerate_id(true);
        header("Location: dashboard.php");
        exit;
        } else {
            echo "<script>alert('Invalid credentials')</script>";
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Log In</title>

<link rel="stylesheet" href="css/style.css" />

</head>

<body>
<!-- <nav><a href="#" class="focus">Log In</a></nav> -->

<form action="index.php" method="POST" enctype="multipart/form-data">

	<h2>Admin Login</h2>

	<input type="text" name="username" class="text-field" placeholder="Username" />
    <input type="password" name="password" class="text-field" placeholder="Password" />
    
  <input type="submit" name="submit" class="button" value="Login" />

</form>

</body>
</html>