Select modules based on Referer URL |
|
|
Have you seen web sites that detect that you found the page via a Google search, and give you some extra information about the site or about what you searched for? This recipe allows to to detect the “referer” that sent the visitor to your site, and gives you the ability to switch modules based on this information. This recipe combines two techniques:
What we’re going to do is to show a particular module when someone lands on our page from a Google search. To make it more user-friendly, we’re just going to show it the first time that this user comes from Google – after that we cookie it in their browser and they’ll never see this module again (at least, from the same browser). /* detect if this user has already seen the module */ if (array_key_exists('DID_GOOGLE_MODULE', $_COOKIE)) return; /* get hold of the "referer" string */ $ref = array_key_exists( 'HTTP_REFERER', $_SERVER ) ? $_SERVER['HTTP_REFERER'] : ''; /* was the referer Google? */ if (substr($ref,0,17) == 'http://www.google') { /* if so, set a cookie for 10 years... */ setcookie('DID_GOOGLE_MODULE',1,time() + 10*365*24*60*60,'/'); /* ... and show the module */ return 1000; /* customise module number to return */ } |


