Get keyword from (search engine) referrer url using PHP

I am trying to get the search keyword from the referrer url. I am currently using the following code for google urls. But sometimes it doesn’t work ...

$query_get = "(q|p)"; $referrer = "http://www.google.com/search?hl=en&q=learn+php+2&client=firefox"; preg_match('/[?&]'.$query_get.'=(.*?)[&]/',$referrer,$search_keyword); 

Is there any other / clean / working way to do this?

Thank you Prasad

+7
url php keyword preg-match
source share
7 answers

If you are using PHP5, see http://php.net/parse_url and http://php.net/parse_str

Example:

 // The referrer
 $ referrer = 'http://www.google.com/search?hl=en&q=learn+php+2&client=firefox';

 // Parse the URL into an array
 $ parsed = parse_url ($ referrer, PHP_URL_QUERY);

 // Parse the query string into an array
 parse_str ($ parsed, $ query);

 // Output the result
 echo $ query ['q'];
+14
source share

Different search engines have different query strings. After trying the Wiliam method, I figured out my own method. (Because Yahoo uses "p", but sometimes "q")

 $referrer = "http://search.yahoo.com/search?p=www.stack+overflow%2Ccom&ei=utf-8&fr=slv8-msgr&xargs=0&pstart=1&b=61&xa=nSFc5KjbV2gQCZejYJqWdQ--,1259335755"; $referrer_query = parse_url($referrer); $referrer_query = $referrer_query['query']; $q = "[q|p]"; //Yahoo uses both query strings, I am using switch() for each search engine preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword); $keyword = urldecode($keyword[1]); echo $keyword; //Outputs "www.stack overflow,com" 

Thank you Prasad

+1
source share

In addition to the other answers, note that the query string parameter containing the search terms depends on the search provider. This PHP snippet shows the correct parameter:

 $search_engines = array( 'q' => 'alltheweb|aol|ask|ask|bing|google', 'p' => 'yahoo', 'wd' => 'baidu', 'text' => 'yandex' ); 

Source: http://betterwp.net/wordpress-tips/get-search-keywords-from-referrer/

+1
source share
 <?php class GET_HOST_KEYWORD { public function get_host_and_keyword($_url) { $p = $q = ""; $chunk_url = parse_url($_url); $_data["host"] = ($chunk_url['host'])?$chunk_url['host']:''; parse_str($chunk_url['query']); $_data["keyword"] = ($p)?$p:(($q)?$q:''); return $_data; } } // Sample Example $obj = new GET_HOST_KEYWORD(); print_r($obj->get_host_and_keyword('http://www.google.co.in/search?sourceid=chrome&ie=UTF-&q=hire php php programmer')); // sample output //Array //( // [host] => www.google.co.in // [keyword] => hire php php programmer //) // $search_engines = array( // 'q' => 'alltheweb|aol|ask|ask|bing|google', // 'p' => 'yahoo', // 'wd' => 'baidu', // 'text' => 'yandex' //); ?> 
+1
source share
 $query = parse_url($request, PHP_URL_QUERY); 
0
source share

This one should work for Google, Bing, and sometimes for Yahoo Search:

 if( isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']) { $query = getSeQuery($_SERVER['HTTP_REFERER']); echo $query; } else { echo "I think they spelled REFERER wrong? Anyways, your browser says you don't have one."; } function getSeQuery($url = false) { $segments = parse_url($url); $keywords = null; if($query = isset($segments['query']) ? $segments['query'] : (isset($segments['fragment']) ? $segments['fragment'] : null)) { parse_str($query, $segments); $keywords = isset($segments['q']) ? $segments['q'] : (isset($segments['p']) ? $segments['p'] : null); } return $keywords; } 
0
source share

I believe that Google and yahoo have updated their algorithm to exclude search keywords and other parameters in the URL that cannot be obtained using the http_referrer method.

Please let me know if the above recommendations will still contain search keywords.

What I get now is below when using http referrer on my site.

from google: https://www.google.co.in/ from yahoo: https://in.yahoo.com/

Link: https://webmasters.googleblog.com/2012/03/upcoming-changes-in-googles-http.html

0
source share

All Articles