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.

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.

                     '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.

11 thoughts to “Grabbing the referrer search engine keywords for a site”

  1. 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.

  2. 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!

Comments are closed.