Select modules based on Referer URL

(8 votes, average 3.88 out of 5)

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:

  1. checking the “referer” string and making decisions based on that
  2. setting and checking cookies, to use these as a memory mechanism for people returning to the site later.

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).

With JomGenius

This technique is available from MetaMod 2.4 onwards.
/* detect if this user has already seen the module */
if (array_key_exists('DID_GOOGLE_MODULE', $_COOKIE)) return;
 
/* was the referer Google? */
if ( $core_genius->check( "referer starts with 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 XXX; /* replace XXX with module id to return */
}

Pre-JomGenius

If you have MetaMod 2.4 or more recent, try the "JomGenius" version instead of this one.
/* 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 XXX; /* replace XXX with module id to return */
}