PHP登录系统问题:点击登录按钮仅刷新页面未跳转至index2.php
Hey there, let's figure out why your login system isn't redirecting to index2.php as expected. I’ve run into this exact issue plenty of times, so let’s walk through the most likely fixes step by step:
First, make sure your HTML form is set up correctly. It should use the POST method (since login data shouldn’t be exposed in the URL) and point to your login.php file in the action attribute. For example:
<form action="login.php" method="POST"> <!-- Your username/password input fields here --> <button type="submit">Login</button> </form>
If you left action empty, some browsers might behave unexpectedly. Also, ensure your submit button is set to type="submit"—if you used a regular button without JavaScript to trigger the form submit, it’ll just refresh the page without sending any login data.
In your login.php code, when the login credentials are verified, you need to make sure the redirect is implemented properly:
session_start()must be called before any output (even a single space before the<?phptag will break this).- Use
header("Location: index2.php");followed immediately byexit();to stop any further code from running.
Here’s what that section should look like:
<?php session_start(); // Critical: No output (spaces, HTML, echoes) before this line! // Database connection and user lookup logic here... if ($login_successful) { // Replace with your actual success check (e.g., matching user in DB) $_SESSION['logged_in_user'] = $username; // Store user identity in session header("Location: index2.php"); exit(); // Don't skip this—it prevents the rest of the script from executing } else { // Show error message like "Invalid username or password" } ?>
If you see a "headers already sent" error (enable error reporting if you don’t), that means there’s unintended output before the header() call—fix this by removing any spaces, HTML, or echo statements before your PHP opening tag or session_start().
It’s possible your login check is failing silently, so the redirect never triggers. Add a quick debug message to see if your code is hitting the "invalid credentials" branch:
// After running your database query to check the user $user_count = mysqli_num_rows($result); // Adjust this for your DB extension (mysqli/PDO) if ($user_count === 1) { // Proceed with login success logic } else { echo "Login failed: No matching username/password found"; // Temporary debug line }
If you see this message when you try to log in, your query is the problem. Check:
- Did you hash the password the same way when the user registered? (The tutorial uses MD5—make sure your registration code uses the same hashing method.)
- Is your database connection working? Add a check for connection errors (e.g.,
mysqli_connect_error()). - Are your SQL query parameters correctly escaped to avoid syntax errors or SQL injection?
Hidden errors are often the culprit. Add these lines at the very top of login.php to reveal all issues:
<?php error_reporting(E_ALL); ini_set('display_errors', 1); session_start(); // Rest of your code ?>
This will show you any problems like database connection failures, undefined variables, or header output errors that might be breaking the redirect.
内容的提问来源于stack exchange,提问作者user9512753

