Jump to content

HTTP_REFERER


MainStWebGuy

Recommended Posts

HEy guys,

I was going over some of the google webmaster tools and analytics and started wondering... Is there a way for me to know what search string a user enters to find my site? I'm pretty new to using php and came across this code snippet that i thought was interesting... does this look like something that will work?

 



$referer = $_SERVER[HTTP_REFERER];

if($referer)
{
    preg_match("/[\&\?]q=([^&]*)/", $referer, $matches);

    if($matches[1])
    {
        $search_query = rawurldecode($matches[1]);
        $search_query = str_replace("+", " ", $search_query);
    }
}

print "You searched Google for '$search_query'";

 

I'd love to hear what some of you gurus think!

 

thanks!

J

Link to comment
Share on other sites

It is your web server log files that should be analysed for search engine keywords. Look at AWStats, Webalizer, etc.. however the $_SERVER['HTTP_REFERER'] will contain the url of the page that referred the user.

 

This part is looking at the url to see if it matches a Google query string:

preg_match("/[\&\?]q=([^&]*)/", $referer, $matches);

 

The keyword is then extracted.

 

http://awstats.sourceforge.net/

http://www.webalizer.org/

 

Link to comment
Share on other sites

Thanks Neil!

 

I'm following that line of thought and would like to am running into a problem. I've modified the code as follows to check and see if the user entered a particular city in their search string and to assign values to a "cityName" variable appropriately.

 

$referer = $_SERVER[HTTP_REFERER];

if($referer)
{
   preg_match("/[\&\?]q=([^&]*)/", $referer, $matches);

   if($matches[1])
   {
       $search_query = rawurldecode($matches[1]);
       $search_query = str_replace("+", " ", $search_query);
   }	
		if (stristr($search_query, 'city1') == TRUE) {
		$cityName = "City1";

	}	elseif (stristr($search_query,'city2') == TRUE) {
		$cityName = "City2";

	}	elseif (stristr($search_query, 'city3') == TRUE) {
		$cityName = "City3";

	}	elseif (stristr($search_query, 'city4') == TRUE) {
		$cityName = "City4";		

	}	else {
		$cityName = "City1, City2, City3, City4 or any other community in between";
	}	
}

 

This all works well, but then i started thinking... what if the visitor reached my page by clicking on a link on another page of my site, or even a link from a referring site?

 

If this happens, would a value to my $cityName variable be assigned at all? If not, can any of you think of a way to assign one?

 

Thanks again!

Link to comment
Share on other sites

No because this line is looking for a Google search string in the referer

   preg_match("/[\&\?]q=([^&]*)/", $referer, $matches);
   if($matches[1]) {

 

You would have to set the value

 

   preg_match("/[\&\?]q=([^&]*)/", $referer, $matches);
   if($matches[1]) {

   }
   else {
      // not a google referer
      $cityName = "abc";
   }

Link to comment
Share on other sites

I hope i'm don't seem lazy with this next question... i'm just having a hard time getting my head around it:

 

First of all... your suggestion worked perfectly, thanks again!

 

Now if a person comes to my site from Google or if they come from a page within my site, the location will be set. Woot woot!

 

However, if they are coming from a referring site, i can't think of a way to assign this location. Now i'm just thinking out loud here, but hypothetically, it would be impossible to know which city to serve if they come from a referring site (or type in the url directly) wouldn't it?

 

would it make sense to add something like

 

<?php
...
} else {
   cityName = $_GET['location']
}

if ($_GET['location'] == FALSE) {
   $cityName = "Default City";
}  

 

Thanks again!

Link to comment
Share on other sites

With this code

   preg_match("/[\&\?]q=([^&]*)/", $referer, $matches);
   if($matches[1]) {
      $cityName = "xyz";
   }
   else {
      // not a google referer
      $cityName = "abc";
   }

If the referer is from google then $cityName will be xyz. If they are refered from anywhere else including your own site then it is set to abc.

 

If you have the city name in the url then you must bypass this code so it does not change the $cityName variable. i.e.

$cityName = (strlen($_GET['location'])) ? $_GET['location'] : false;
// no location passed through url - check the referer
if(!$cityName) {
   preg_match("/[\&\?]q=([^&]*)/", $referer, $matches);
   if($matches[1]) {
      $cityName = "xyz";
   }
   else {
      // not a google referer
      $cityName = "abc";
   }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.