What potential pitfalls should be considered when using $_SERVER['HTTP_REFERER'] in PHP?

Using $_SERVER['HTTP_REFERER'] in PHP can be risky because it relies on client-side data that can be easily manipulated. This can lead to security vulnerabilities such as spoofing and injection attacks. To mitigate these risks, it is important to validate and sanitize the data before using it in your application.

$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

// Validate and sanitize the referer URL
$referer = filter_var($referer, FILTER_VALIDATE_URL);

// Use the sanitized referer URL in your application
echo $referer;