Current Location: Home> Latest Articles> How to initialize a custom error page in the init function in PHP?

How to initialize a custom error page in the init function in PHP?

gitbox 2025-05-20

In PHP development, setting custom error pages is an important task to improve the user experience. Typically, we can handle PHP errors and exceptions through the set_error_handler() and set_exception_handler() functions and forward them to a custom error page. This article will initialize these error handling mechanisms through the init function in PHP and display the error message on a user-friendly custom page.

What is an init function?

In PHP, the init function is usually an initialization function executed when the program starts. It is usually used to set some basic configurations, such as database connection, error handling, cache settings, etc. We can use this function to initialize the error handler to ensure that there is a unified error handling logic throughout the application life cycle.

Step 1: Create a custom error page

First, we need to prepare a custom error page. For example, we can create a simple HTML page that displays error messages.

 <!-- error_page.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Error page</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f2f2f2;
            color: #333;
            text-align: center;
            padding: 50px;
        }
        h1 {
            color: #f44336;
        }
    </style>
</head>
<body>
    <h1>Feel sorry,An error occurred!</h1>
    <p>We are working hard to fix the problem,Please try again later。</p>
</body>
</html>

This page will serve as a display page for all our errors, and users will see a concise and friendly prompt.

Step 2: Write the init function and initialize the error handling

Next, in PHP's init function, we can set up a custom error handler. With the set_error_handler() and set_exception_handler() functions, we can catch PHP errors and exceptions and redirect them to our custom error page.

 <?php
// init.php

// Initialize function
function init() {
    // Setting up custom error handlers
    set_error_handler('customErrorHandler');
    
    // Set up custom exception handlers
    set_exception_handler('customExceptionHandler');
}

// Custom error handler
function customErrorHandler($errno, $errstr, $errfile, $errline) {
    // You can log error logs or send emails to notify developers
    error_log("Error [$errno]: $errstr in $errfile on line $errline");

    // 重定向到自定义Error page
    header("Location: https://gitbox.net/error_page.html");
    exit();
}

// Custom exception handler
function customExceptionHandler($exception) {
    // Can log exception logs or send emails to notify developers
    error_log("Uncaught exception: " . $exception->getMessage());

    // 重定向到自定义Error page
    header("Location: https://gitbox.net/error_page.html");
    exit();
}

// 调用Initialize function
init();
?>

Step 3: Handle Errors and Exceptions

In the above code, the init() function is called at the start of the application and two error handlers are set: customErrorHandler and customExceptionHandler .

  • set_error_handler('customErrorHandler') allows PHP errors to be redirected to the customErrorHandler function to handle.

  • set_exception_handler('customExceptionHandler') allows uncaught exceptions to be redirected to the customExceptionHandler function to handle.

When an error or exception occurs, PHP will automatically execute these functions, record relevant logs, and redirect the user to the https://gitbox.net/error_page.html page. In this way, users will not see PHP's default error page, but our carefully designed error page.

Step 4: Test the Error Handler

To test our error handler, some errors or exceptions can be deliberately thrown in the code. For example, call an undefined function or throw an exception:

 <?php
// A mistake is deliberately raised
echo undefinedFunction();

// A special exception is thrown
throw new Exception("This is a test exception!");
?>

When accessing this page, the user should be redirected to the https://gitbox.net/error_page.html page without seeing the PHP default error message.

Summarize

By initializing the custom error page in the init function in PHP, we can ensure that the application's error handling mechanism is unified and friendly. By setting up custom error handlers and exception handlers, we can display all error messages on custom HTML pages through redirection, improving the user experience and reducing the risk of leaking sensitive information.

Hope this article helps you better understand how to configure custom error pages in PHP. If you have other questions or suggestions for improvement, please leave a message below to discuss!