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

When I search for something like " rearrange table columns in asp.net

" on Google and click on the Wrox forum forum link , the site greets me with a message like "Your Google search for" reordering table columns in asp.net "brought you to the Wrox forum .. . ".

How does the site know which query I entered into Google? And how can I add such an absorption to my site?

+2


source to share


4 answers


Parses your request from request parameters into a server variable HTTP_REFERER

that contains the url you're sending from and provided in your HTTP request.



+12


source


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

To use it on your site, you will need dynamic page creation such as ASP / ASP.NET, PHP or Perl. For example, in Perl, you can do something like:



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

      

WARNING : the above code is just an example and may be wrong or safe!

+8


source


Like these guys, this is a title variable HTTP_REFERER

. The request is in the "q" key in the URL. So if you want to parse this, you can just sort the query string and the url to decode the "q" variable.

+2


source


He looks at the referrer header. Here is some pretty simple PHP code to do this.

+1


source







All Articles