<?php
session_start();

// Database connection parameters
$host = "localhost";
$username = "earthour_camgtpu";
$password = "dh+z9*R*V0mR";
$database = "earthour_campaignsgtp";

// Create a database connection
$conn = new mysqli($host, $username, $password, $database);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Process login form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST["username"];
    $password = $_POST["password"];

    // Query the database to check if the user exists
    $sql = "SELECT * FROM tusers WHERE username = '$username' AND password = '$password'";
    $result = $conn->query($sql);

    if ($result->num_rows == 1) {
        // User exists, set a session variable to indicate login
        $_SESSION["loggedin"] = true;
        $_SESSION["username"] = $username;

        // Redirect to a protected page after successful login
        header("Location: dash.php");
    } else {
        // Invalid credentials, show an error message
        header("Location: login.php?error=0&errorm=Invalid username or password.");
    }
}

// Close the database connection
$conn->close();
?>
