<?php
session_start
();

// Connect to the database (using your credentials and database name)
$LINK mysqli_connect('127.0.0.1''tyrfl709''[q8OXd*BEbxW6bM4''tyrfl709');
if (!
$LINK) {
    die(
"Database connection error: " mysqli_connect_error());
}

$error '';

if (isset(
$_POST['login'])) {
    
// Get username and password from form and sanitize them
    
$username mysqli_real_escape_string($LINK$_POST['username']);
    
$password mysqli_real_escape_string($LINK$_POST['password']);
    
    
// Query the database for the user with the given username.
    
$query "SELECT * FROM users WHERE username = '$username' LIMIT 1";
    
$result mysqli_query($LINK$query);
    
    if (
$result && mysqli_num_rows($result) > 0) {
        
$user mysqli_fetch_assoc($result);
        
// For simplicity, we're comparing plaintext passwords.
        // In a real application, use password_verify() with hashed passwords.
        
if ($password === $user['password']) {
            
// Login successful; set session variables.
            
$_SESSION['logged_in'] = true;
            
$_SESSION['username'] = $username;
            
header("Location: cms.php");
            exit();
        } else {
            
$error "Invalid password.";
        }
    } else {
        
$error "User not found.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login - CMS</title>
</head>
<body>
    <h1>Login to CMS</h1>
    <?php if ($error): ?>
        <p style="color:red;"><?php echo htmlspecialchars($error); ?></p>
    <?php endif; ?>
    <form action="login.php" method="POST">
        <label for="username">Username:</label>
        <input type="text" name="username" id="username" required>
        <br><br>
        <label for="password">Password:</label>
        <input type="password" name="password" id="password" required>
        <br><br>
        <button type="submit" name="login">Login</button>
    </form>
</body>
</html>