Custom message for people browsing from Google

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
studley
Forum Newbie
Posts: 5
Joined: Fri Dec 31, 2004 5:10 am

Custom message for people browsing from Google

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

$_SERVER['HTTP_REFERER'] .. look into it.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

ya right! $_SERVER['HTTP_REFERER'] is the right thing to do in 1st place
studley
Forum Newbie
Posts: 5
Joined: Fri Dec 31, 2004 5:10 am

Post 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
idotcom
Forum Commoner
Posts: 69
Joined: Thu Mar 04, 2004 9:24 pm

Post 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!";
}


?>
?>
studley
Forum Newbie
Posts: 5
Joined: Fri Dec 31, 2004 5:10 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

be VERY careful variable variables that come from outside the source.. like the referrer.
idotcom
Forum Commoner
Posts: 69
Joined: Thu Mar 04, 2004 9:24 pm

Post by idotcom »

Very true...

but that code only fires if it finds google in the url. Use parse_url to confirm host, domain, etc.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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.
Post Reply