You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP登录页面开发报错求助:Cannot POST及文件包含等问题

Fixing Your PHP Login Page Issues

Let's work through your problems step by step to get your basic login functionality up and running:

1. Fix the "Cannot POST /pages/login.html" Error

This is your first critical issue: static HTML files can’t handle POST requests. Your login form is trying to submit data to login.html, but browsers only allow GET requests for static HTML files—they can’t process form submissions directly.

Quick Fix:

Update your login.html form’s action attribute to point to your PHP processing file instead:

<form action="../php/login.php" method="POST">
    <label for="username">Username:</label>
    <input type="text" id="username" name="username" required>
    
    <label for="password">Password:</label>
    <input type="password" id="password" name="password" required>
    
    <button type="submit">Login</button>
</form>

2. Fix File Inclusion Path Errors

The warnings about failed file opens happen because you’re using absolute paths that don’t match your XAMPP setup. Your project lives in C:\xampp\htdocs\Left_over_youth_website, so paths starting with /left_over_youth_website/ are pointing to the wrong root (that’s a filesystem root, not your web project root).

Reliable Solution: Use __DIR__ for Paths

__DIR__ gives you the absolute directory of the current PHP file, so you can build paths relative to that without guessing:

  • In login.php, to include Connection.php (same directory):
    include __DIR__ . '/Connection.php';
    
  • To include login.html (one directory up, then into the pages folder):
    include __DIR__ . '/../pages/login.html';
    

3. Fix Database Connection & Undefined Variable Errors

All the Undefined variable: db and mysqli function errors stem from Connection.php not loading correctly. First, make sure your Connection.php properly creates a database connection:

Example Connection.php:

<?php
// XAMPP default credentials (adjust if you set a root password)
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'leftoveryouth';

// Create and validate the connection
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$db) {
    die("Database connection failed: " . mysqli_connect_error());
}
?>

Once this is correctly included in login.php, the $db variable will exist, and your mysqli functions will work as expected.

4. Fix Undefined Index Warnings

The Undefined index: username/password warnings pop up when you try to access $_POST values that don’t exist (like when the page loads via GET instead of POST). Add a check for POST requests first to avoid this:

Updated login.php Logic:

<?php
include __DIR__ . '/Connection.php';

// Only process form data if it's a POST request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Grab form values (skipping security for now as requested)
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $password = mysqli_real_escape_string($db, $_POST['password']);

    // Query the clients table
    $query = "SELECT * FROM clients WHERE username = '$username'";
    $result = mysqli_query($db, $query);

    if ($result && mysqli_num_rows($result) > 0) {
        $user = mysqli_fetch_array($result);
        // Simple password match (use hashing in production!)
        if ($user['password'] === $password) {
            session_start();
            $_SESSION['user'] = $user;
            header('Location: welcome.php');
            exit; // Always exit after a redirect to stop further code execution
        } else {
            echo "Incorrect password!";
            include __DIR__ . '/../pages/login.html';
        }
    } else {
        echo "User not found!";
        include __DIR__ . '/../pages/login.html';
    }
} else {
    // Show the login form if it's a GET request
    include __DIR__ . '/../pages/login.html';
}
?>

Key Recap of Fixes

  • Point your form’s action to a PHP file, not an HTML file
  • Use __DIR__ to avoid path confusion when including files
  • Ensure your database connection loads before using mysqli functions
  • Only process form data when the request is a POST

内容的提问来源于stack exchange,提问作者Hazaki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:22:44