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 */

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at] for any new project requirements and price quotes.

11 Responses


Sameer Borate Blog: Wir packen die Referrer Suchmaschinen-Keywords für eine Website | PHP Boutique

October 18th, 2011 at 2:04 pm

[…] 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 […]


Anonymous Coward

October 19th, 2011 at 8:54 am

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


Managed Forex Accounts

November 6th, 2011 at 12:09 pm

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


Allan Slogan

December 1st, 2011 at 1:31 pm

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.



August 31st, 2012 at 4:07 am

Can i grap https:// ?


August 31st, 2012 at 4:48 am

It should.



October 3rd, 2012 at 1:55 am

Hi Does this still work?


Facebook Cerca

October 12th, 2012 at 3:18 am

Probabely google changed the url referrarl structure :-(


October 29th, 2012 at 3:10 am




January 24th, 2013 at 3:15 am

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



January 31st, 2013 at 6:43 am

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!

Your thoughts

Sign up for fresh content in your email