<?php
session_start();

// Password Settings - Change these as needed
define('PASSWORD', 'admin123'); // Change this password
define('SESSION_TIMEOUT', 1800); // 30 minutes

// Check if user is logged in
function checkLogin() {
    if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
        return false;
    }
    
    // Check session timeout
    if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > SESSION_TIMEOUT)) {
        session_unset();
        session_destroy();
        return false;
    }
    
    $_SESSION['last_activity'] = time();
    return true;
}

// Handle login form submission
function handleLogin() {
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['password'])) {
        if ($_POST['password'] === PASSWORD) {
            $_SESSION['logged_in'] = true;
            $_SESSION['last_activity'] = time();
            return true;
        } else {
            echo "<p style='color: red;'>Incorrect password!</p>";
            return false;
        }
    }
    return false;
}

// Display login form
function showLoginForm() {
    echo '
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Login Required</title>
        <style>
            body { 
                font-family: Arial, sans-serif; 
                background: #f0f0f0; 
                display: flex; 
                justify-content: center; 
                align-items: center; 
                height: 100vh; 
                margin: 0; 
            }
            .login-container { 
                background: white; 
                padding: 40px; 
                border-radius: 10px; 
                box-shadow: 0 0 10px rgba(0,0,0,0.1); 
                text-align: center; 
            }
            input[type="password"] { 
                padding: 10px; 
                margin: 10px 0; 
                width: 200px; 
                border: 1px solid #ddd; 
                border-radius: 5px; 
            }
            button { 
                padding: 10px 20px; 
                background: #007bff; 
                color: white; 
                border: none; 
                border-radius: 5px; 
                cursor: pointer; 
            }
            button:hover { 
                background: #0056b3; 
            }
            .logout-btn {
                background: #dc3545;
                margin-left: 10px;
            }
            .logout-btn:hover {
                background: #c82333;
            }
        </style>
    </head>
    <body>
        <div class="login-container">
            <h2>Login Required</h2>
            <form method="POST">
                <input type="password" name="password" placeholder="Enter password" required>
                <br>
                <b