使用 PHP 获取原始 URL 引用?

发布于 2024-08-13 19:02:04 字数 114 浏览 5 评论 0原文

我正在使用 $_SERVER['HTTP_REFERER']; 来获取引用网址。它按预期工作,直到用户单击另一个页面并且引用站点更改为最后一页。

如何存储原始引用 URL?

I am using $_SERVER['HTTP_REFERER']; to get the referer Url. It works as expected until the user clicks another page and the referer changes to the last page.

How do I store the original referring Url?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

肤浅与狂妄 2024-08-20 19:02:04

将其存储在 cookie 中(如果您的情况可以接受)或会话变量中。

session_start();

if ( !isset( $_SESSION["origURL"] ) )
    $_SESSION["origURL"] = $_SERVER["HTTP_REFERER"];

Store it either in a cookie (if it's acceptable for your situation), or in a session variable.

session_start();

if ( !isset( $_SESSION["origURL"] ) )
    $_SESSION["origURL"] = $_SERVER["HTTP_REFERER"];
柠栀 2024-08-20 19:02:04

正如 Johnathan 所建议的,您要么希望将其保存在 cookie 中,要么保存在会话中。

更简单的方法是使用会话变量。

session_start();
if(!isset($_SESSION['org_referer']))
{
    $_SESSION['org_referer'] = $_SERVER['HTTP_REFERER'];
}

将其放在页面顶部,您将始终能够访问网站访问者所引导的第一个引用者。

As Johnathan Suggested, you would either want to save it in a cookie or a session.

The easier way would be to use a Session variable.

session_start();
if(!isset($_SESSION['org_referer']))
{
    $_SESSION['org_referer'] = $_SERVER['HTTP_REFERER'];
}

Put that at the top of the page, and you will always be able to access the first referer that the site visitor was directed by.

很酷不放纵 2024-08-20 19:02:04

将其存储在仅持续当前浏览会话的 cookie 中

Store it in a cookie that only lasts for the current browsing session

你穿错了嫁妆 2024-08-20 19:02:04

在大多数情况下,使用 Cookie 作为参考页面的存储库要好得多,因为 Cookie 会保留引用来源,直到浏览器关闭(并且即使浏览器选项卡关闭也会保留它),因此,如果用户让页面保持打开状态,那么可以说在周末之前,几天后返回,您的会话可能会过期,但 cookie 仍然存在。

将该代码放在页面的开头(在任何 html 输出之前,因为只有在任何 echo/print 之前才会正确设置 cookie):

if(!isset($_COOKIE['origin_ref']))
{
    setcookie('origin_ref', $_SERVER['HTTP_REFERER']);
}

然后您可以稍后访问它:

$var = $_COOKIE['origin_ref'];

除了 @pcp 建议的关于转义 $_SERVER[' 的内容之外HTTP_REFERER'],当使用cookie时,您可能还想在每个请求上转义$_COOKIE['origin_ref']。

Using Cookie as a repository of reference page is much better in most cases, as cookies will keep referrer until the browser is closed (and will keep it even if browser tab is closed), so in case if user left the page open, let's say before weekends, and returned to it after a couple of days, your session will probably be expired, but cookies are still will be there.

Put that code at the begin of a page (before any html output, as cookies will be properly set only before any echo/print):

if(!isset($_COOKIE['origin_ref']))
{
    setcookie('origin_ref', $_SERVER['HTTP_REFERER']);
}

Then you can access it later:

$var = $_COOKIE['origin_ref'];

And to addition to what @pcp suggested about escaping $_SERVER['HTTP_REFERER'], when using cookie, you may also want to escape $_COOKIE['origin_ref'] on each request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文