How does a website know the Google query I used to find it?

China☆狼群 提交于 2019-12-08 00:55:47

问题


When I search for something such as "rearrange table columns in asp.net" on Google, and click the link to Wrox's forum site, the site greets me with a message such as "Your Google search for 'rearrange table columns in asp.net' brought you to Wrox Forum...".

How does a site know what query I typed into Google? And how could I add such an ablity to my site?


回答1:


It is parsing your query from the query parameters in the HTTP_REFERER server variable, which contains the URL you're coming from and is provided in your HTTP request.




回答2:


It uses a header known as the "HTTP referrer". See http://en.wikipedia.org/wiki/HTTP_referrer

To use it in your site, you would need some kind of dynamic page generation, such as ASP / ASP.NET, PHP, or Perl. For example in Perl, you could do something like:

if ($ENV{HTTP_REFERER} =~ /google.com\?.+&q=(.+?)&/)
    print "Your google search of $1 brought you to this site";

WARNING: The code above is only an example and may not be correct or secure!




回答3:


Like these guys are suggesting, it's the HTTP_REFERER header variable. The query is in the "q" key in the URL. So if you want to parse that, you can just sort out the querystring and URL decode the "q" variable.




回答4:


It looks at the referrer header. Here is some fairly basic PHP code to do it.



来源:https://stackoverflow.com/questions/1348206/how-does-a-website-know-the-google-query-i-used-to-find-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!