0

可能有很多关于 SO 的相关问题和很多关于它的文章,但我无法理解它的原因。

我想要实现的是我们有一些附属的东西。如果用户点击我们在谷歌或其他任何地方的任何广告。他被带到一个页面,我们将他重定向到适当的页面。对于重定向,我们使用以下代码。

header("HTTP/1.1 301 Moved Permanently");  
header("Location: http://www.example.com/page2.php");

假设这段代码被写入page1.php并且用户被重定向到page2.php. 在page1我获得用户到达的地方(例如 google.com)的引荐来源。但是在page2.php我们没有得到引用为page1. 在我看来,这是一种奇怪的行为。任何人都可以阐明这种行为的原因。

PS:我通过在重定向之前添加一个 cookie 并在page2.

谢谢

4

2 回答 2

0

如果我没记错的话,引荐来源网址是由浏览器发送的(如果允许,并非所有浏览器都这样做,并且客户端可以轻松更改它),这意味着您的 page1 永远不会设置为第 2 页的引荐来源网址,因为它是服务器进行重定向,而不是客户端。据我所知,没有办法“伪造”它,虽然 bansi 提出的技术看起来很诱人,但我在网上看到多个帖子表明它实际上不起作用。

于 2013-07-31T13:40:53.833 回答
-2

IMPORTANT : as noted by @cryptic and @Bartdude The following solution is tempting but won't work. Why this won't work is explained below.

--- Original solution ---
Add Referer header also

header("Referer: http://www.the_referer_page");

The Referer is sent by the browser to the server. Because you are redirecting to another site you have to manually send the header to the other site as there is no browser involved.

--- Original solution end ---

Why the above solution wont work

The HTTP referer is sent by the browser to the server. When the 301 Moved Permanently header is sent to the browser the server is telling the browser that this page is moved and load the page from the new location. It is then the browser's decision to request or don't request the page from the new location and what all the headers to be sent to the new server. Actually sending a HTTP referer header to the browser is really meaningless.

Sorry for the wrong solution provided. Hope this avoids someones temptation to use the Referer header as it tempted me.

于 2013-07-31T13:38:24.510 回答