使用 PHP 获取原始 URL 引用?
我正在使用 $_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将其存储在 cookie 中(如果您的情况可以接受)或会话变量中。
Store it either in a cookie (if it's acceptable for your situation), or in a session variable.
正如 Johnathan 所建议的,您要么希望将其保存在 cookie 中,要么保存在会话中。
更简单的方法是使用会话变量。
将其放在页面顶部,您将始终能够访问网站访问者所引导的第一个引用者。
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.
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.
将其存储在仅持续当前浏览会话的 cookie 中
Store it in a cookie that only lasts for the current browsing session
在大多数情况下,使用 Cookie 作为参考页面的存储库要好得多,因为 Cookie 会保留引用来源,直到浏览器关闭(并且即使浏览器选项卡关闭也会保留它),因此,如果用户让页面保持打开状态,那么可以说在周末之前,几天后返回,您的会话可能会过期,但 cookie 仍然存在。
将该代码放在页面的开头(在任何 html 输出之前,因为只有在任何 echo/print 之前才会正确设置 cookie):
然后您可以稍后访问它:
除了 @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):
Then you can access it later:
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.