Detect PHP Errors When Running Make PHP Code from an HTML File

I’ve been working with PHP for about six months now, and recently I faced detect PHP errors when running make PHP code from an html file a situation I need help with. Here’s what’s going on:

I’m running a login.html file on my web server, and within this HTML file, there’s a section that triggers a PHP function. This PHP function is stored in an external file called functions.php.

The issue arises when something goes wrong within the PHP code. If there’s an error — especially a fatal error — it doesn’t get caught by my custom error handler class. Instead, all I see is the login attempt failing on the HTML front-end, without any information about what actually went wrong in the PHP function.

I’m wondering if there’s a way to track or log these errors. Is there a specific log file I can check, or some configuration I need to set up to catch these issues when PHP runs within an HTML file?

I would appreciate any tips or feedback on how I can improve error tracking for these cases.

Here’s how you can structure an example to fit the explanation above:

Example: PHP Login Function Triggered from HTML File

HTML File (login.html):

This file contains a basic form that triggers a login function.

code<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Page</title>
</head>
<body>
<form action="functions.php" method="POST">
<input type="text" name="username" placeholder="Enter Username" required>
<input type="password" name="password" placeholder="Enter Password" required>
<button type="submit">Login</button>
</form>
</body>
</html>

PHP Function File (functions.php):

This file handles the login logic, but let’s say it contains a fatal error.

code<?php
// Example: Login function with a mistake (Fatal Error)
function loginUser($username, $password) {
// Intentional mistake: Undefined function 'connectToDatabase'
$db = connectToDatabase(); // This function doesn't exist
// Assuming database login query follows
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];

loginUser($username, $password); // Calling the function with inputs
}
?>

Problem Observed:

When a user submits the login form on the login.html page, the page might reload or just show a failed login attempt with no clear error message. This happens because the fatal error (calling an undefined connectToDatabase() function) isn’t caught by your error handler class.

How to Track Errors: To view the actual error, you can enable PHP error logging by adding the following code at the top of your functions.php file:

code<?php
ini_set('display_errors', 1); // Display errors on the page (for development only)
ini_set('display_startup_errors', 1);
error_reporting(E_ALL); // Report all errors

// Alternatively, log errors to a file:
ini_set('log_errors', 1);
ini_set('error_log', 'path/to/error.log'); // Replace with the correct log path
?>

Expected Outcome:

With this setup, if a user encounters the undefined function error, it will either:

Display the error on the page (for easier debugging during development).

Log the error in a specified log file (recommended for production environments).

This example illustrates the type of situation you described and provides a way to track errors effectively. It helps you catch the issue early rather than just seeing the failed login attempt with no error details on the front-end.

Related blog posts