Fix PHP Blank Screen Error in Login Script

I’m working on connecting an HTML login page to a SQL database using PHP, and everything seemed to be progressing smoothly until I encountered a frustrating issue. Now, when I run the code or submit the login form, I’m faced with a blank white screen instead of any feedback, success message, or error notification. I’ve tried enabling error reporting to troubleshoot, but nothing appears on the screen. I suspect it might be something simple I’m overlooking or a hidden error, but I can’t pinpoint it. Here’s the PHP code I’m using:

Error Code:

code<?php
error_reporting(-1);
// Check if form was submitted
if (isset($_POST['submit'])) {
// Check if email and password fields are not empty
if (!empty($_POST['email']) && !empty($_POST['password'])) {
// Get POST data
$email = $_POST['email'];
$password = $_POST['password'];

// Database Connection
$con = new mysqli("localhost", "thor", "******", "thor_rspca");

if ($con->connect_error) {
die("Failed to connect: " . $con->connect_error);
} else {
$stmt = $con->prepare("SELECT * FROM User WHERE email = ?");
if ($stmt) {
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt_result = $stmt->get_result();

if ($stmt_result->num_rows > 0) {
$data = $stmt_result->fetch_assoc();

if ($data['password'] === $password) {
echo "Login Successful";
} else {
echo "Invalid Email or Password";
}
} else {
echo "Invalid Email or Password";
}
} else {
echo "Database Error";
}
}
} else {
echo "Please fill in all fields";
}
}
?>

Here is the corrected PHP code and an explanation of how it works to avoid the blank screen error. Some common issues that cause a blank screen include missing or misconfigured error reporting, incorrect database credentials, or problems with the SQL query. Here’s the corrected code with explanations.

Corrected Code:

code<?php
// Enable error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Check if form was submitted
if (isset($_POST['submit'])) {
// Check if email and password fields are not empty
if (!empty($_POST['email']) && !empty($_POST['password'])) {
// Get POST data
$email = $_POST['email'];
$password = $_POST['password'];

// Database Connection
$con = new mysqli("localhost", "thor", "******", "thor_rspca");

// Check for connection errors
if ($con->connect_error) {
die("Failed to connect: " . $con->connect_error);
} else {
// Prepare SQL statement
$stmt = $con->prepare("SELECT * FROM User WHERE email = ?");
if ($stmt) {
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt_result = $stmt->get_result();

// Check if user exists and validate password
if ($stmt_result->num_rows > 0) {
$data = $stmt_result->fetch_assoc();

// Use password hashing for comparison (recommended for security)
if (password_verify($password, $data['password'])) {
echo "Login Successful";
} else {
echo "Invalid Email or Password";
}
} else {
echo "Invalid Email or Password";
}
} else {
echo "Database Error: Unable to prepare statement";
}
}
} else {
echo "Please fill in all fields";
}
}
?>

Explanation:

  1. Error Reporting:
    • Added ini_set('display_errors', 1); and ini_set('display_startup_errors', 1); to ensure PHP errors are displayed.
    • Set error_reporting(E_ALL); to enable all error messages, making troubleshooting easier.
  2. Password Hashing:
    • It is generally insecure to store plaintext passwords in a database. The password_verify() function is used here to compare the entered password with a hashed password stored in the database.
    • Ensure passwords are stored in a hashed format using password_hash() when initially adding them to the database.
  3. Database Connection Check:
    • The $con->connect_error checks for connection issues and outputs an error message if the connection fails, helping to identify if the issue is with the connection.
  4. Statement Preparation Check:
    • The code checks if $stmt is created successfully. If not, it outputs “Database Error: Unable to prepare statement,” making it easier to debug if the statement preparation fails.
  5. Error in SQL Query:
    • If you’re still facing issues, check that the User table and email column exist in your database with the correct names and case.
  6. Tips for Debugging a Blank Screen:
    • If errors are not showing, verify that your server’s php.ini file has display_errors set to On.
    • Use echo statements at different points in your code to trace its execution flow, which can help locate the issue.

Final Thoughts:

The always enable error reporting, check database connections thoroughly, and use secure password handling with hashing to avoid blank screen issues in PHP. Debugging carefully and securing your code will lead to a smoother, more reliable login experience.

Related blog posts