Skip to content
PHP Logo

How can a PHP CMS detect and record the original request that led a bot to a 404 page?

When a bot encounters a 404 page on a PHP CMS, the CMS can detect and record the original request by capturing the requested URL before redirecting to the 404 page. This can be achieved by storing the requested URL in a session variable or database along with the timestamp for further analysis or logging.

php
// Check if the requested page is not found
if ($page_not_found) {
    // Get the original requested URL
    $requested_url = $_SERVER['REQUEST_URI'];

    // Store the requested URL in a session variable
    $_SESSION['original_request'] = $requested_url;

    // Log the requested URL along with timestamp in a database
    $timestamp = time();
    $log_entry = "404 Error - Requested URL: $requested_url - Timestamp: $timestamp";
    // Code to log $log_entry in a database
}