地址栏输入与链接
有没有办法询问请求以确定导航方法?
(使用 LAMP 配置,PHP)
换句话说...我想知道该条目是手动输入到地址栏中的,还是使用了链接/书签。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有没有办法询问请求以确定导航方法?
(使用 LAMP 配置,PHP)
换句话说...我想知道该条目是手动输入到地址栏中的,还是使用了链接/书签。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
您所能做的就是查看
$SERVER['HTTP_REFERER']
[原文如此]。如果您是直接链接的,那么您很有可能会在那里找到链接到您的页面的 URL。但在很多情况下您无法获得它,因此您不能依赖它。在 IE 中,如果您之前使用过 saveFavorite 行为,用于在用户为您添加书签之前保留一些数据。但它非常丑陋。
About all you can do is look at the
$SERVER['HTTP_REFERER']
[sic]. If you were directly linked, there is a good chance you will find the URL of the page that linked to you there. There are many circumstances where you won't get it though, so you can't rely on it.In IE it is possible to detect when you are loaded from a bookmark, if you previously used a saveFavorite behaviour to persist some data before the user bookmarked you. But it's pretty ugly.
您可以检查
$_SERVER['HTTP_REFERER']
变量,其中包含用户导航的 URL。我不确定如果他们选择书签或手动输入 URL 会发生什么,但我怀疑此变量将为空。不过,正如手册中所述:
You can check the
$_SERVER['HTTP_REFERER']
variable which will contain the URL the person has navigated from. I'm not sure what happens if they choose a bookmark or enter the URL manually, though I suspect that this variable would be blank.Though, as it says in the manual:
可靠的是,你不能。
您可以尝试的一种方法是引用标头(这是规范中的拼写方式),通常由浏览器发送以指示用户来自哪里。在 PHP 中,这可以在
$_SERVER['HTTP_REFERER']
中找到。大多数现代浏览器(我测试过 Google Chrome 和 Firefox)会在链接点击时发送引用标头,但在直接输入时不会发送引用标头 - 也就是说,如果用户的设置要求这样做,则默认情况下通常会这样做。
另一种选择(取决于您真正关心的程度)是将某种随机生成的会话变量附加到链接中的 URL,并在收到请求时查看 URL 中的令牌是否与用户会话中的令牌匹配。如果是这样,那就这样吧。如果没有,则该链接是复制粘贴的。
当然,所有这些方法都很容易被击败,并且您的服务器也可能被愚弄。不要依赖他们。
Reliably, you can not.
One method you could try is referer headers (which is how it's spelled in the spec), usually sent by the browser to indicate where the user is coming from. In PHP, this is available in
$_SERVER['HTTP_REFERER']
.Most modern browsers (I've tested Google Chrome and Firefox) send a referer header on link clicks and not on direct entry - that is, if the user's settings say to, which they usually do by default.
Another option, depending on just how much you really care, is to append some sort of randomly-generated session variable to the URL in the link and, on receiving a request, see if the token in the URL matches the token in the user's session. If so, there you go. If not, this link was copy-pasted.
Of course, all of these methods can easily be beaten, and your server can be fooled. Don't depend on them.
如果他们单击链接,浏览器将设置引荐来源网址,您可以通过使用
并检查是否已设置来检测引荐来源网址标头是否存在。如果为空,则表示该人已输入 URL 或使用浏览器扩展来清除引荐来源网址。这大约是您在这个问题上所能找到的最接近的解决方案。
If they click a link the browser will set a referrer, you could detect the presence of a referrer header by using
and checking to see if that has been set. If it's blank the person has either typed in the URL or has a browser extension to clear referrers. It's about as close as you'll get to a solution on this one.