Recording incoming keywords from google searches.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thud
Forum Newbie
Posts: 3
Joined: Fri Jan 26, 2007 10:23 pm

Recording incoming keywords from google searches.

Post by thud »

I need to record incoming keywords from google searches.

Ive seen websites that do this but I am unsure about how they are doing it.

Are any of you familiar with this?
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Post by William »

$_SERVER['HTTP_REFERER'] ?
thud
Forum Newbie
Posts: 3
Joined: Fri Jan 26, 2007 10:23 pm

Post by thud »

I am trying to record incoming keywords from google searches.

So far I have this code (Im new)

Code: Select all

<?php
echo $_SERVER['HTTP_REFERER'];
?>



For example, this is where I would grab the keyword from -

http://search.yahoo.com/search?p=KEYWOR ... -8&fr=moz2

Can someone provide me with the code to do this?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

HTTP_REFERER is an attribute defined by which website people come to yours from. Printing it to your screen will, at most, show you where you came from, which is useless information to you.

You could use cookies or sessions and log your visitor's referers into a log file or, preferably, a database. You say you're new so I don't expect you to automatically know how to do this, but I hope I've at least got you thinking on the right track.

I'd help you with the code, but I'm busy with a client's site. I'm sure someone will respond with either more detail or a better, more sensible method.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

By the way, when attempting to test it, make sure you're aware that there doesn't always have to be a referrer. You must come from elsewhere (I believe it doesn't matter whether or not you arrive through a link).
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Ignore superdezign, he clearly didn't understand what you're after. "You could use cookies or sessions and log your visitor's referers into a log file" just doesn't make any sense. Cookies and sessions have nothing whatsoever to do with logging someone's referer.

To get the search information out of the refering string from the users request header you'll need to use a couple of functions that PHP provides for breaking down URLs, namely parse_url() and parse_str().

Code: Select all

<?php

	$referer = $_SERVER['HTTP_REFERER'];

	$arrRefererComponents = parse_url($referer);
	parse_str($arrRefererComponents['query'],$arrGetVars);

	switch($arrRefererComponents['host']) {
		case "yahoo.com": 
		case "www.yahoo.com": 
		case "search.yahoo.com": 
			echo $arrGetVars['p'];
			break;
		case "google.co.uk":
		case "www.google.co.uk":
		case "google.com":
		case "www.google.com":
			echo $arrGetVars['q'];
			break;
	}

?>
You might want to expand that code to work more flexibly with the host names. Perhaps you could write something to strip the TLD (.com, .co.uk, etc) off so you don't have to worry about that part of it. Obviously you'll also need to write something to log the search term rather than echoing it too.
Last edited by onion2k on Sat Jan 27, 2007 6:57 am, edited 1 time in total.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

You could use the stristr function if you don't care about the TLD

Code: Select all

$referer = $_SERVER['HTTP_REFERER'];
if(stristr($referer, 'google.')) {

  $query = $_GET['q'];
  //do you insert or something else here

}elseif(stristr($referer, 'yahoo.')){

  $query = $_GET['p'];
  //do you insert or something else here

}
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You can't use $_GET for variables in the referer.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

my mistake there. I wasn't thinking straight having some trouble with some arrays.

also shouldn't you code be:

Code: Select all

$referer = $_SERVER['HTTP_REFERER']; 

//instead of 

$referer = $_SERVER['HTTP_HOST'];
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I have no idea what you mean. ;)
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

No hard feelings.
I know how it feels. :wink:
thud
Forum Newbie
Posts: 3
Joined: Fri Jan 26, 2007 10:23 pm

Post by thud »

Wonderful.

Now my only difficulty is recording the links... I'd like to write/read the links to/from a flat txt file on the server.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

if you have a database wouldn't be easier to add them to a table there, by using an INSERT statement?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

thud wrote:Now my only difficulty is recording the links... I'd like to write/read the links to/from a flat txt file on the server.
Try to work it out for yourself.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Bleh.

Yeah, I was thinking you take the entire referer URL, put it into a session variable, and store the value into a database. I was thinking "session" because I thought it'd be the only way to prevent you from getting the referer more than once per user.

However, it does make sense to just check for if it came from a search engine first. I wasn't even thinking that much. I was emulating what webstats do for you... Yeah, ignore superdezign. He didn't know what he was talking about. :(
Post Reply