当有人点击搜索结果链接时,Google会设置HTTP推荐人。此引荐来源与URL地址栏中显示的地址不同(例如,它包含GET参数cd,表示所点击链接的搜索结果位置)。
似乎这是用javascript完成的。我想在我的网站上做类似的事情,所以我很想知道这个“引用者操纵”是如何用javascript完成的。
有什么想法吗?
答案 0 :(得分:3)
当您点击其结果链接时,Google会通过自己的点击代理重定向您 - 代理地址就是您在Referer
中看到的内容。
示例:我转到http://www.google.cz并搜索了“当有人点击搜索结果链接时,Google如何设置HTTP推荐人?”。这导致了这个要求:
GET http://www.google.cz/search?hl=cs&source=hp&biw=1276&bih=866&q=How+does+Google+set+the+HTTP+Referrer+when+someone+clicks+on+a+search+result+link%3F&aq=f&aqi=&aql=&oq=&gs_rfai=&fp=b29a84a7dd59af16 HTTP/1.1
Referer: http://www.google.cz/
从那里,我点击了第一个结果链接:How does Google set the HTTP Referrer when someone clicks on a search result link? 该点击被JS事件捕获并重新路由到此重定向器:
GET http://www.google.cz/url?sa=t&source=web&cd=1&ved=0CBoQFjAA&url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F4402502%2Fhow-does-google-set-the-http-referrer-when-someone-clicks-on-a-search-result-link&rct=j&q=How%20does%20Google%20set%20the%20HTTP%20Referrer%20when%20someone%20clicks%20on%20a%20search%20result%20link%3F&ei=WTgBTeOXLsHB8QPO44ybCA&usg=AFQjCNE22KabWH5TnkK1sRLGmqWQ4EvwxQ HTTP/1.1
Referer: http://www.google.cz/search?hl=cs&source=hp&biw=1276&bih=866&q=How+does+Google+set+the+HTTP+Referrer+when+someone+clicks+on+a+search+result+link%3F&aq=f&aqi=&aql=&oq=&gs_rfai=&fp=b29a84a7dd59af16
包含此重定向代码段:
<body><a href="https://stackoverflow.com/questions/4402502/how-does-google-set-the-http-referrer-when-someone-clicks-on-a-search-result-link" id=link target=_parent></body><script>var a=parent,b=parent.google,c=location;if(a!=window&&b){if(b.r){b.r=0;document.getElementById("link").click();}}else{document.getElementById("link").click();};</script><noscript><META http-equiv="refresh" content="0;URL='https://stackoverflow.com/questions/4402502/how-does-google-set-the-http-referrer-when-someone-clicks-on-a-search-result-link'"></noscript>
最终将我发送到真实网址:
GET https://stackoverflow.com/questions/4402502/how-does-google-set-the-http-referrer-when-someone-clicks-on-a-search-result-link HTTP/1.1
Referer: http://www.google.cz/url?sa=t&source=web&cd=1&ved=0CBoQFjAA&url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F4402502%2Fhow-does-google-set-the-http-referrer-when-someone-clicks-on-a-search-result-link&rct=j&q=How%20does%20Google%20set%20the%20HTTP%20Referrer%20when%20someone%20clicks%20on%20a%20search%20result%20link%3F&ei=WTgBTeOXLsHB8QPO44ybCA&usg=AFQjCNE22KabWH5TnkK1sRLGmqWQ4EvwxQ
所以,你是对的 - 这里的大部分繁重都是通过JavaScript完成的,虽然也涉及一些服务器端代码。