What are some potential pitfalls when using $_SERVER['HTTP_REFERER'] in PHP?

Using $_SERVER['HTTP_REFERER'] can be unreliable as it relies on the client's browser to send the referrer information. This information can be easily manipulated or stripped out, leading to inaccurate results. To mitigate this issue, consider using an alternative method for tracking referrers, such as using session variables or passing referrer information through hidden form fields.

// Check if the referrer information is set and sanitize it
$referrer = isset($_SERVER['HTTP_REFERER']) ? filter_var($_SERVER['HTTP_REFERER'], FILTER_SANITIZE_URL) : '';

// Use the sanitized referrer information in your application
echo 'Referrer: ' . $referrer;