地址栏输入与链接

发布于 2024-08-20 07:52:59 字数 96 浏览 2 评论 0 原文

有没有办法询问请求以确定导航方法?

(使用 LAMP 配置,PHP)

换句话说...我想知道该条目是手动输入到地址栏中的,还是使用了链接/书签。

Is there a way to interrogate a request to determine the method of navigation ?

(using a LAMP configuration, PHP)

In other words... I would like to know whether the entry was hand typed into the address bar, or a link/bookmark was used.

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

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

发布评论

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

评论(4

ま昔日黯然 2024-08-27 07:53:00

您所能做的就是查看 $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.

七秒鱼° 2024-08-27 07:52:59

您可以检查 $_SERVER['HTTP_REFERER'] 变量,其中包含用户导航的 URL。我不确定如果他们选择书签或手动输入 URL 会发生什么,但我怀疑此变量将为空。

不过,正如手册中所述:

将用户代理引导至当前页面的页面地址(如果有)。这是由用户代理设置的。并非所有用户代理都会设置此项,有些用户代理提供了修改 HTTP_REFERER 的功能。简而言之,它确实不能被信任。

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:

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

审判长 2024-08-27 07:52:59

可靠的是,你不能。

您可以尝试的一种方法是引用标头(这是规范中的拼写方式),通常由浏览器发送以指示用户来自哪里。在 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.

你不是我要的菜∠ 2024-08-27 07:52:59

如果他们单击链接,浏览器将设置引荐来源网址,您可以通过使用

$_SERVER['HTTP_REFERER'] 

并检查是否已设置来检测引荐来源网址标头是否存在。如果为空,则表示该人已输入 URL 或使用浏览器扩展来清除引荐来源网址。这大约是您在这个问题上所能找到的最接近的解决方案。

If they click a link the browser will set a referrer, you could detect the presence of a referrer header by using

$_SERVER['HTTP_REFERER'] 

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.

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