Get keyword from a (search engine) referrer url using PHP

I am trying to get the search keyword from a referrer url. Currently, I am using the following code for Google urls. But sometimes it is not working...

$query_get = "(q|p)";
$referrer = "";

Is there another/clean/working way to do this?

Thank you, Prasad


If you're using PHP5 take a look at and


// The referrer
$referrer = '';

// 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'];

There are different query strings on different search engines. After trying Wiliam's method, I have figured out my own method. (Because, Yahoo's is using 'p', but sometimes 'q')

$referrer = ",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
$keyword = urldecode($keyword[1]);
echo $keyword; //Outputs "www.stack overflow,com"

Thank you, Prasad

To supplement the other answers, note that the query string parameter that contains the search terms varies by search provider. This snippet of PHP shows the correct parameter to use:

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


    public function get_host_and_keyword($_url) { 
        $p = $q = "";
        $chunk_url = parse_url($_url); 
        $_data["host"] = ($chunk_url['host'])?$chunk_url['host']:''; 
        $_data["keyword"] = ($p)?$p:(($q)?$q:''); 
        return $_data; 
// Sample Example 
$obj = new GET_HOST_KEYWORD(); 
print_r($obj->get_host_and_keyword(' php php programmer')); 

// sample output
//    [host] =>
//    [keyword] => hire php php programmer

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


$query = parse_url($request, PHP_URL_QUERY);

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

    $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;

I believe google and yahoo had updated their algorithm to exclude search keywords and other params in the url which cannot be received using http_referrer method.

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

What I am receiving now are below when using http referrer at my website end.

from google: from yahoo:


Need Your Help

Java server to Android client object Serialization

java android serialization

I'm trying to serialize a class Message from a java server and send them to an Android phone.

Bulk rename folders and subfolders with '/' in shared mailbox using powershell

email powershell exchange-server office365

I am using IMAP to login my users to my organization's shared Office365 mailboxes from their iPhones and Android tablets, since I cannot use Exchange ActiveSync to allow these users to read the sha...