Home > Backend Development > PHP Tutorial > How to Preserve the Original Referer URL in PHP?

How to Preserve the Original Referer URL in PHP?

Linda Hamilton
Release: 2024-10-19 14:27:02
Original
948 people have browsed it

How to Preserve the Original Referer URL in PHP?

Getting the Original URL Referer in PHP

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.

Preserving the Original Referer

To address this issue and retain the original referring URL, you have two primary options:

Using Cookies

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.

Using Session Variables

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>
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template