LogIn (login.php) Code Example

Copy the code below:

<?php
session_start();
require_once 'C:/inetpub/config_nm.php'; // Include your config with user/pass

$error = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';

    if (
        $username === Auth_User &&
        password_verify($password, Auth_User_Pass)
    ) {
        $_SESSION['username'] = $username;
		$_SESSION['logged_in'] = true;
        header('Location:index.php');
        exit;
    } else {
        $error = 'Invalid username or password.';
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<?php 
  $pageTitle = "Login Page"; 
  include 'includes/head.php'; 
?>
<body class="bg-light">

<div class="container mt-5">
  <div class="row justify-content-center">
    <div class="col-md-4">
      <div class="card shadow">
        <div class="card-body">
          <h3 class="card-title text-center mb-4">Login</h3>

          <?php if ($error): ?>
            <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
          <?php endif; ?>

          <form method="POST" action="">
            <div class="mb-3">
              <label for="username" class="form-label">Username</label>
              <input type="text" name="username" class="form-control" id="username" required>
            </div>

            <div class="mb-3">
              <label for="password" class="form-label">Password</label>
              <input type="password" name="password" class="form-control" id="password" required>
            </div>

            <div class="d-grid">
              <button type="submit" class="btn btn-primary">Login</button>
            </div>
          </form>

        </div>
      </div>
    </div>
  </div>
</div>
 <?php include 'includes/scripts.php'; ?>
</body>
</html>