Page 1 of 1

Custom message for people browsing from Google

Posted: Fri Dec 31, 2004 5:17 am
by studley
Hi, I'm new in these parts (and fairly new to PHP) so go easy on me :)

I've started to notice that my website is receiving a lot more "random" visitors from Google; i.e people typing in peculiar searches and my site ranking highly in the search results.

#1 What I would like to do is put some code in my header.php file which detects whether a user has arrived on my site from Google. (And if so, I can display a nice message for them, welcoming them specifically based on this - I like the personal touch.)

#2 In an ideal world I would also like to be able to write their search criteria to the page (something like "You searched on Google for [search string]"). eg if they had searched for 'devnetwork', their Google URL would be http://www.google.co.uk/search?sourceid ... devnetwork - so in the message I display on the page, I would like to be able to say "You searched on Google for devnetwork".

If anyone can give me some pointers for #1 I'd be very appreciative. #2 is way above anything I'm capable of at present, so if anyone fancies the challenge of writing it for me, or just pointing me in the right direction, I'd be forever grateful!

Thanks,
Neil

Posted: Fri Dec 31, 2004 7:25 am
by onion2k
$_SERVER['HTTP_REFERER'] .. look into it.

Posted: Fri Dec 31, 2004 8:29 am
by n00b Saibot
ya right! $_SERVER['HTTP_REFERER'] is the right thing to do in 1st place

Posted: Sun Jan 02, 2005 1:47 pm
by studley
Thanks guys! All it took was a little experimentation and I soon found that this worked:
if (isset($_SERVER['HTTP_REFERER']) && strpos(strtolower($_SERVER['HTTP_REFERER']), 'google'))
{

*** message here ***

}
And while I haven't worked out how to do #2, I easily managed to put a link back to the user's Google search by echoing the referer string.

If you're interested in seeing it in action, try visiting my site directly by clicking here (there will be no Google message), and then click here and click on the first search result - you will see my Google message.

Thanks!
Neil

Posted: Sun Jan 02, 2005 2:10 pm
by idotcom
Hi
For #2

something like...

Code: Select all

<?php

$thisurl=$_SERVER['HTTP_REFERER'];

if (isset($thisurl) && strpos(strtolower($thisurl), 'google')) 
{ 
 $tab=parse_url($thisurl);
 $query=$tab["query"];
 $variables=explode("&",$query);
 for ($i=0;$i<=count($variables);$i++){
     $tab=explode("=",$variables[$i]);
   $$tab[0]=$tab[1];

 }
//variable variables are cool 
//Now you have all the variables in the url. so...

echo "You searched google for $q and found us!";
}


?>
?>

Posted: Sun Jan 02, 2005 2:18 pm
by studley
That works a treat, thanks! :) Now to figure out for myself exactly why it works. I guess the "explode" thing is the key, I've not seen or used that before.

Cheers,
Neil

Posted: Sun Jan 02, 2005 2:21 pm
by feyd
be VERY careful variable variables that come from outside the source.. like the referrer.

Posted: Sun Jan 02, 2005 5:17 pm
by idotcom
Very true...

but that code only fires if it finds google in the url. Use parse_url to confirm host, domain, etc.

Posted: Sun Jan 02, 2005 5:22 pm
by rehfeld
idotcom wrote:Very true...

but that code only fires if it finds google in the url. Use parse_url to confirm host, domain, etc.
that wont help if someone crafts thier own referer header, which is very easy to do. its just a bad idea to import variables from outside php into the global namespace. yes its unlikely, but many security holes that get exploited were unlikely too.

instead of using variable variables to parse out the query string i would use parse_str()

first use parse_url()
then use parse_str() on the query string indice


now you have the variables in an array that you can safely name.