Home  >  Article  >  Backend Development  >  How to Preserve the Original Referring URL in PHP?

How to Preserve the Original Referring URL in PHP?

Barbara Streisand
Barbara StreisandOriginal
2024-10-19 14:22:02680browse

How to Preserve the Original Referring URL in PHP?

Finding the Original Referring URL with PHP

When utilizing $_SERVER['HTTP_REFERER'] to determine the originating URL, it efficiently operates until a user navigates to a separate page, causing the referrer to reference the previous page. To preserve the original referer URL, consider storing it in either a cookie or a session variable. This ensures that the initial source of the user's visit remains accessible.

Explanation:

To preserve the original referring URL during subsequent page interactions, you can store it in a cookie. This is particularly useful if you need to keep track of the user's source page throughout the website session. Using a session variable, on the other hand, achieves the same goal but confines the saved data within a single PHP session.

Implementation:

The following code exemplifies the use of session variables:

<code class="php"><?php
session_start();
if ( !isset( $_SESSION["origURL"] ) ) {
    $_SESSION["origURL"] = $_SERVER["HTTP_REFERER"];
}
?></code>

By initiating the session with session_start() and verifying whether the "origURL" session variable is undefined, this code stores the original referring URL in the session. This ensures that the original source of the user's visit remains accessible throughout their interactions with the website within the same session.

The above is the detailed content of How to Preserve the Original Referring URL in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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