Grabbing the referrer search engine keywords for a site

A couple of weeks back I had to write a solution for a client to track the referrer search engine from where the user came to his sites contact page, without using Google Analytics. If a user was to fill the contact form on the website, the referring search engine name and the keyword for which it was refereed was to be emailed along with the contact information. The following is a solution for the same.

To get the referrer information we need to use the $_SERVER[‘HTTP_REFERER’] variable and store it in a session so that it is not overwritten as the user browses the site before he submits the contact information. If the users comes to the website from a search engine he could land on any page, so we need to add some code to capture the referrer url on each page of the site. We only need to save the referrer if the referrer is from outside the current domain.

The following is the code that need to be added to each web page on the website. The referrer url will then be stored in a session and carried along as the user browses the site.

/* saveReferer.php */
$my_site = "";
/* If the referrer is from outside the domain, store the url in a session */
if(!substr($_SERVER['HTTP_REFERER'], 0, strlen($my_site)) !== $my_site) {
    if(!isset($_SESSION['my_referer'])) {
        $_SESSION['my_referer'] = $_SERVER['HTTP_REFERER'];

You can now include the above file in all your web pages.


To grab the search keyword from the referrer url we can use the get_keyword function given below.

function get_keyword($referer)
    $search_phrase = '';
    $engines = array('dmoz'     => 'q=',
                     'aol'      => 'q=',
                     'ask'      => 'q=',
                     'google'   => 'q=',
                     'bing'     => 'q=',
                     'hotbot'   => 'q=',
                     'teoma'    => 'q=',
                     'yahoo'    => 'p=',
                     'altavista'=> 'p=',
                     'lycos'    => 'query=',
                     'kanoodle' => 'query='
    foreach($engines as $engine => $query_param) {
        // Check if the referer is a search engine from our list.
        // Also check if the query parameter is valid.
        if (strpos($referer, $engine.".") !==  false && 
            strpos($referer, $query_param) !==  false) {
            // Grab the keyword from the referer url
            $referer .= "&";
            $pattern = "/[?&]{$query_param}(.*?)&/si";
            preg_match($pattern, $referer, $matches);
            $search_phrase = urldecode($matches[1]);
            return array($engine, $search_phrase);

The following is a sample contact page which sends the referrer search engine name and the keyword to the website admin.

/* contact.php */
$engine = '';
$search_keyword = '';
$data = get_keyword($_SESSION['my_referer']);
if($data) {
   $engine = $data[0];
   $search_keyword = $data[1];
$message  = "SOME MESSAGE HERE\r\n";
$message .= "Engine : $engine \n";
$message .= "Keyword: $search_keyword \n";
$headers .= "From: ". "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail("", "Test Email", $message, $headers);
/* Make sure you reset the referrer session variable */

11 Responses

  1. Anonymous Coward says:

    This won’t be useful for too much longer:
    After they move all logged in users to https you’ll only get referer search terms per thousand visitors

  2. i really get a good info from your blog, thank so much

  3. Allan Slogan says:

    I too enjoyed this article, as I’ve been trying to figure out how to properly dig for more referrer code when people come to the site. In particular, I’d like to discover how to monitor traffic that is too fast. In other words, a particluar IP will cruise dozens of pages in about 20 seconds. Then I could know that it’s a bot snagging information and cut it off.

  4. Sudarsono says:

    Can i grap https:// ?

  5. Martin says:

    Hi Does this still work?

  6. Probabely google changed the url referrarl structure :-(

  7. Petras says:

    That’s what I was looking for a long time. Thank you very much.

  8. David says:

    You will NOT be able to grab the search terms used when a user is logged into his Google Account. So that means that you wont be able to grab the search term when the Google search page has https:// in the URL. It’s secured, so forget it!

  1. October 18, 2011

    […] Blog heute Sameer Borate hat einen neuen Beitrag mit einem handlichen bisschen Code, den Sie auf find die Schlüsselwörter aus einer Suchmaschine Verweis , um mit Tracking, wie die Besucher auf Ihre Website kommen helfen. Ein paar Wochen zuvor hatte ich […]