Having worked with search engines for more than 5 years, I can tell you there's no standard way to retrieve the query value.
As other answers already told you, the first step is to inspect the HTTP_REFERER
header. Assuming you are using Rails, you can get it from the request
request.referrer
Otherwise, you need to extract it from request headers in an other way.
Once you have the referrer, then you are in front of 3 main possibilities:
- variable is empty. sorry, you can't do nothing
- variable is not empty, it's a search engine
- variable is not empty, it's not a search engine
The first option is simple. What you want to know is if the referrer is a search engine. If so, then you need to extract the query.
The most common way to do this is using a checklist. The checklist is usually a list of key/value where the key is the search engine domain and the value the name of the query string parameter that holds the query value.
google.com,q
yahoo.com,p
...
This is the same approach used by Google Analytics. From the ga.js file
g.T=l("daum:q,eniro:search_word,naver:query,images.google:q,google:q,yahoo:p,msn:q,bing:q,aol:query,aol:encquery,lycos:query,ask:q,altavista:q,netscape:query,cnn:query,about:terms,mamma:query,alltheweb:q,voila:rdata,virgilio:qs,live:q,baidu:wd,alice:qs,yandex:text,najdi:q,aol:q,mama:query,seznam:q,search:q,wp:szukaj,onet:qt,szukacz:q,yam:k,pchome:q,kvasir:q,sesam:q,ozu:q,terra:query,mynet:q,ekolay:q,rambler:words");
First host matches both key and value, first wins.