How can one ensure that the HTTP_REFERER value is not spoofed or manipulated by users?

To ensure that the HTTP_REFERER value is not spoofed or manipulated by users, you can compare the value of the HTTP_REFERER header with the actual URL of the previous page. This can be done by storing the URL of the previous page in a session variable and then comparing it with the HTTP_REFERER value when needed.

session_start();

// Store the URL of the previous page in a session variable
$_SESSION['previous_url'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

// Compare the stored URL with the HTTP_REFERER value
if ($_SESSION['previous_url'] !== $_SERVER['HTTP_REFERER']) {
    // Handle the case where the HTTP_REFERER value is spoofed or manipulated
}