When utilizing $_SERVER['HTTP_REFERER'] to retrieve the referer URL, it accurately captures the expected URL until the user navigates to a different page, causing the referer to update to the previous address.
To address this issue and retain the original referring URL, you have two primary options:
If acceptable for your scenario, consider storing the referer URL in a cookie. This approach allows you to persist the URL across multiple page visits.
Alternatively, you can utilize PHP's session variables to store the referer URL. Session variables maintain their value throughout a browsing session, making them suitable for this purpose.
<code class="php"><?php session_start(); // Check if the original URL is already stored in the session if ( !isset( $_SESSION["origURL"] ) ) { // If not, assign the current referer URL to the session variable $_SESSION["origURL"] = $_SERVER["HTTP_REFERER"]; } ?></code>
The above is the detailed content of How to Preserve the Original Referer URL in PHP?. For more information, please follow other related articles on the PHP Chinese website!