1

I want to make several uses of the search phrase that users used in google in order to reach my site.

Is there any programmatic way to do that?

Thanks, Oran

OrPaz
  • 1,065
  • 11
  • 25

4 Answers4

5

You can use the code below:

$referrer = $_SERVER['HTTP_REFERER']; 
if (!empty($referrer)) { 
    $parts_url = parse_url($referrer); 
    $query = isset($parts_url['query']) ? $parts_url['query'] : ''); 
        if($query) { 
            parse_str($query, $parts_query);             

            $ref_keywords = isset($parts_query['q']) ? $parts_query['q'] : (isset($parts_query['query']) ? $parts_query['query'] : '' )); 
        }
}

However, note that Google has now stopped sending the query string in their referrer for all logged in users. So your functionality will work only for users who are logged out of Google . Here is more information on this http://googleblog.blogspot.com/2011/10/making-search-more-secure.html

Virendra
  • 2,560
  • 3
  • 23
  • 37
4

Use the HTTP_REFERER server variable.

$referer= $_SERVER['HTTP_REFERER'];
//find the search query from google that brought them here
$qref= strpos($referer,’google’);
if($qref!=”){
$qstart = strpos($referer,’q=’);
$qend = strpos($referer,’&’,$qstart);
$qtext= substr($referer,$qstart+2,$qend-$qstart-2);
$qtext= str_replace(‘+’,’ ‘,$qtext);
}
echo $qtext

Taken from here.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • 1
    It should be noted that `strpos` returns boolean false if the string is not found, and an integer if it is found. So you should use `if($qref !== false){` – Arjan Jan 28 '12 at 21:41
  • Does this work anymore now that Google transferred over to https and https supposedly doesn't pass HTTP_REFERER data? – Michael d Nov 25 '15 at 10:57
1

I reckon it should be in the referrer, as the they append the search string to the url via the q-parameter.

karllindmark
  • 6,031
  • 1
  • 26
  • 41
1

You can get it from the "HTTP_referer" in the request.($_SERVER['HTTP_REFERER'];)

see also another question regarding the topic

Community
  • 1
  • 1
Aba Dov
  • 962
  • 4
  • 12
  • 20