What are the limitations of using the HTTP_REFERER variable for dynamically generating URLs in PHP?

The HTTP_REFERER variable can be unreliable as it relies on the client's browser to send the referring URL, which can be manipulated or blocked. To ensure more consistent results, it's recommended to use a session variable or a hidden form field to pass the necessary information between pages.

// Using a session variable to store and retrieve the referring URL
session_start();
$_SESSION['referring_url'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

// Accessing the stored referring URL on the next page
$referring_url = isset($_SESSION['referring_url']) ? $_SESSION['referring_url'] : '';