$_SERVER['HTTP_REFERER'] 和 cookies

发布于 2024-10-18 09:42:43 字数 764 浏览 3 评论 0原文

我在 SO 和其他网站上读到, $_SERVER['HTTP_REFERER'] 是我们作为程序员应该始终避免的事情。阅读 PHP 手册我们会遇到以下几行:

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

考虑到“用户代理”,例如Web浏览器(Safari、Chrome、Opera、Firefox,基本上都是),手册告诉我们这个变量可能会被它们更改。 这是我的第一个问题:

问题1:为什么“用户代理”要修改这个参数?网页浏览器不设置此参数的原因是什么?

这只是好奇心,我不会使用 $_SERVER['HTTP_REFERER']。 从我最新的声明中可以看出,如果我们确实需要这样的功能,我们就需要弄清楚一些事情。我首先想到的是 cookie 解决方案,在该解决方案中,我们会执行以下操作:

setcookie('latest_page', __FILE__, 60 * 60 * 24 * 7);

然后,根据您喜欢的方式,您可以恢复所需的内容。 这是第二个问题,也是最后一个问题:

问题2:这种方式是最好的吗?有什么办法可以改善吗?

I read, both on SO and other sites, that $_SERVER['HTTP_REFERER'] is something we, as programmers, should always avoid. Reading the PHP manual we encounter these lines:

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.

Considering "user agents" things such as Web Browser (Safari, Chrome, Opera, Firefox, basically they are all), the manual tell us that this variable may be changed by them.
Here it comes my first question:

Question 1: Why should "users agent" modify this parameter? What are the reason for a web browser not to set this parameter?

It's just inquisitiveness, and i'll not use $_SERVER['HTTP_REFERER'].
From my latest statement, it's oblivious that, if we do need of such a function, we need to figure something out. The first thing that comes in my mind is a cookie solution, in which we do something like:

setcookie('latest_page', __FILE__, 60 * 60 * 24 * 7);

And then, based on how you prefer to do it, you can recover what you need.
Here it comes the second and last question:

Question 2: Is this way the best? Are there any way to improve it?

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

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

发布评论

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

评论(7

悲念泪 2024-10-25 09:42:43

没有理由避免HTTP_REFERER。只是在使用时要注意,它并不总是被设置的,并且它可以被客户端自由操作,因此它是不可信数据。

绝大多数客户端都会设置该变量,并且设置正确。

阻止它的主要原因是隐私:例如,当在网络邮件客户端中打开电子邮件时,外部图像的链接将在 HTTP_REFERER 标头中携带网络邮件服务的地址。这就是为什么 GMail 和 Yahoo 努力阻止它

您建议的解决方法仅适用于同一网站内的移动,如果用户打开多个选项卡/浏览器窗口来浏览您的网站,该解决方法将会中断。如果您需要知道引用页面,更好的方法是添加一个 GET 参数,例如

<a href="otherpage.html?from=thispage.html">

Edit: 依赖 HTTP_REFERER 是危险的一个示例是在您的主页上显示实时更新的引用站点列表页面(“访问者来自...”),通过使用虚假的 HTTP_REFERER 集访问您的网站,可以轻松地将任意 URL 偷偷添加到该列表中。

There is no reason to avoid HTTP_REFERER. Just when using it, be aware that it's not always set, and that it can be freely manipulated by the client, so it is untrusted data.

The vast majority of clients sets the variable, and does so correctly.

The main reason for blocking it is privacy: For example, when opening an E-Mail in a web mail client, links to external images would carry the web mail service's address in the HTTP_REFERER header. That's why GMail and Yahoo make efforts to block it.

The workaround you suggest works only for movements within the same site, and will break if the user has more than one tab/browser window open with which they browse your site. If you need to know the referring page, the superior method is to add a GET parameter like

<a href="otherpage.html?from=thispage.html">

Edit: One example where relying on HTTP_REFERER is dangerous would be showing a live-updated list of referring sites on your main page ("Visitors came from...") it would be easy to smuggle arbitrary URLs into that list by visiting your site with a fake HTTP_REFERER set.

骷髅 2024-10-25 09:42:43

为什么“用户代理”要修改这个参数?网页浏览器不设置此参数的原因是什么?

HTTP 引荐来源网址可用于分析用户的行为,因为您可以看到引用当前页面的页面。它还可以向第三方站点公开敏感数据,例如 URL 中的会话 ID。这就是为什么许多网站使用 dereferrer 作为所有外部链接的单个退出页面。

这是某些用户代理根本不发送 HTTP 引用站点的主要原因。与此相反,仅当您实际点击链接或在页面上提交表单时才会发送 HTTP 引荐来源网址。以其他方式触发的任何请求(HTTP 重定向、通过位置栏/书签的直接请求)都不是引荐来源网址。

这种方式是最好的吗?有什么办法可以改善吗?

如果您在多个选项卡/窗口中打开一个网站,那么这样做将不起作用。您需要为每个页面请求提供一个标识符,以便您可以识别实际引用此页面的页面:

  • 当前请求的页面:/index.html
  • 当前请求的页面的请求 ID:12345

每个链接将包含请求 ID 来标识引用下一页的请求:

<a href="/foo.html?request-id=12345">
<a href="/bar.html?request-id=12345">

通过在表单上执行此操作,并另外使用不可预测的页面请求 ID 值,还可以使 CSRF 攻击变得更加困难。

Why should "users agent" modify this parameter? What are the reason for a web browser not to set this parameter?

The HTTP referrer can be used to profile a user’s behavior because you can see the page that referred to the current page. It can also unveil sensitive data such as a session ID in the URL to third party sites. This is why many web sites use dereferrer as a single exit page for all external links.

That are the main reasons why some user agents don’t send a HTTP referrer at all. In opposite to that, HTTP referrers are only send when you actually follow a link or submit a form on a page. Any request that was triggered otherwise (HTTP redirect, direct request via location bar/bookmark) is not a referrer.

Is this way the best? Are there any way to improve it?

Doing it that way won’t work if you have opened a website in multiple tabs/windows. You would need an identifier for each page request so that you can identify the page that actually referred this one:

  • Current requested page: /index.html
  • Request ID of current requested page: 12345

Each link will then contain the request ID to identify the request that referred to the next page:

<a href="/foo.html?request-id=12345">
<a href="/bar.html?request-id=12345">

By doing this also on forms and by additionally using an unpredictible value for the page request ID you can also make CSRF attacks more difficult.

原来分手还会想你 2024-10-25 09:42:43

基于 Cookie 的解决方案不是最好的,也不是解决方案。
没有人使用它,因为 cookie 是一个站点范围的问题。

大多数时候“返回”链接毫无用处。每个现代浏览器都有一个后退按钮。
如果您无论如何都想实现它,只需按照在其他站点上实现的方式进行即可。

Cookie based solution is not the best nor the solution at all.
Nobody using it, because a cookie is a site-wide matter.

Most of time "go back" link is just useless. Every modern browser has a Back button.
If you want to implement it anyway, just make it the same way as it's implemented on the other sites.

好久不见√ 2024-10-25 09:42:43

如果您需要返回链接,您应该使用:

<a href="javascript:history.back();"><< Go Back</a>

或者

<a href="javascript:history.go(-1);"><< Go Back</a>

这是向用户提供返回链接的最佳方式

If you need a go back link you should use this:

<a href="javascript:history.back();"><< Go Back</a>

OR

<a href="javascript:history.go(-1);"><< Go Back</a>

This is the best way to provide a go back link to users

又爬满兰若 2024-10-25 09:42:43

好吧,请记住,就像客户端设置的任何标头一样,它可以被篡改。所以要明智地使用它。

Well, remember that like any header the client sets, it can be tampered with. So use it wisely.

成熟的代价 2024-10-25 09:42:43

如果 Http 引荐来源网址未经清理而必须对您的网站数据库执行某些操作,则可能会很危险。它也完全是客户端,因此有人可以利用它。但除此之外并没有什么问题。

Cookie 也可以更改。如果您想要用户无法更改的内容,请使用 Php 会话。

Http referrer can be dangerous if it has to do something with your website database if not sanitized. It is also completely client side, so there is chance that someone can exploit it. But besides that there is nothing wrong with it.

Cookies can be changed too. If you want something that can not be changed by user go with Php Sessions.

哭泣的笑容 2024-10-25 09:42:43

问题1:为什么要“用户代理”
修改这个参数?有哪些
网络浏览器未设置的原因
这个参数?

嗯,网络浏览器确实没有理由不设置这个。您被告知要避免它,因为某些浏览器会避免它的原因相同,即滥用。就像在 URL 中保存重要的会话信息一样。

你不应该试图避免使用它,你应该负责任地使用它。并且不必使用任何“噱头”。例如,将其用作“恢复”导航是很常见的。例如,某人使用您的网页的会话已过期。您可以将它们发送到登录凭据页面。然后将它们重定向到请求的 URL。

当他们尝试首次访问您的网页而不通过登录屏幕时,情况也是如此。

  1. 启动浏览器(cookie 或会话中没有保存任何信息)
  2. 转到 www.YourApp.com/restrictedZone.php
  3. 重定向到登录
  4. 登录挑战已通过
  5. 重定向到限制区域而不是“index.php”或“home.php”

您应该确保您在所有“限制区域”页面上检查有效会话。

Question 1: Why should "users agent"
modify this parameter? What are the
reason for a web browser not to set
this parameter?

Well, web browsers don't really have a reason not to set this. You've been told to avoid it because of the same reason some browsers would avoid it, wich is misuse. Like saving important session info in the URL.

You shouldn't try and avoid it using, you should just use it responsibly. And not have to use any 'gimmick'. For example, its very common to use it as a 'Resume' navigation. Say for instance someones session expired using your webpage. You could send them to the login credentials page. And then redirect them to the requested URL.

The same goes when they are trying to first access your webpage without passing through the login screen.

  1. Start browser (No info saved in cookies o sessions)
  2. Go to www.YourApp.com/restrictedZone.php
  3. Redirect to login
  4. Login challenge passed
  5. Redirect to restricted zone instead of 'index.php' or 'home.php'

You should just make sure you check for a valid session on ALL of your 'restricted zone' pages.

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