You can create a custom 404 error page in PHP by using the following code in your web server configuration file or in the .htaccess file:
ErrorDocument 404 /404.php
where "/404.php" is the path to your custom 404 error page.
Then, you can create the 404.php file and add the following code to display a custom error message:
Php code
<?php
header("HTTP/1.0 404 Not Found");
?>
<!DOCTYPE html>
<html>
<head>
<title>404 Error Page</title>
</head>
<body>
<h1>404 Error Page</h1>
<p>The page you requested could not be found.</p>
</body>
</html>
You can also add custom styles and graphics to make the error page look more visually appealing.
Additionally, you can use PHP to log the details of the missing page and user information, to help you understand why the 404 error is being generated. For example, you can use the $_SERVER array to retrieve information such as the requested URL, user agent, and referrer, and write that information to a log file or database for analysis.
Example
<?php
header("HTTP/1.0 404 Not Found");
$url = $_SERVER['REQUEST_URI'];
$referer = $_SERVER['HTTP_REFERER'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
// Log the 404 error for analysis
error_log("404 Error: Page not found. URL: $url. Referer: $referer. User Agent: $user_agent.");
?>
<!DOCTYPE html>
<html>
<head>
<title>404 Error Page</title>
</head>
<body>
<h1>404 Error Page</h1>
<p>The page you requested could not be found.</p>
</body>
</html>
This is just an example and you can customize it according to your needs.