Include this PHP file on any pages where you need to check if it is safe to serve an advert.
<?php
/*
* googlesafe.php by Chris Newland @chriswhocodes www.chrisnewland.com
* Functions to help integrate Google AdSense into your website
* and remove the risk of your account being disabled due to
* violation of the AdSense terms and conditions.
* Free to copy and modify
*/


global $bannedIPs;
global $bannedReferrerDomains;
global $bannedURIs;

//=======================================
// Change these values to suit your needs
//=======================================
$bannedIPs = array('127.0.0.1', '80.70.60.', '100.100.100.100', '200.200.200.200');
$bannedReferrerDomains = array('facebook.com', 'facebook.net');
$bannedURIs = array('login', 'logout', 'unknown', 'thankyou');

/*
* Determines whether it is safe to show an advert based on
* 1) The IP address of the client (is it your work or home IP etc)
* 2) The referrer domain (did the client arrive from a facebook link etc)
* 3) If the page URI is on a list where adverts should not be shown (login, logout, unknown, etc)
* 4) If you are in "administrator mode" (if your website has one)
*
* Returns true if it is safe to show an advert, false otherwise.
*/

function canShowAd($pageURI)
{
   global $bannedIPs;
   global $bannedReferrerDomains;
   global $bannedURIs;
   
   $clientIP = $_SERVER['REMOTE_ADDR'];
   $referrer = $_SERVER['HTTP_REFERER'];

   //==========================================
   // Check if client IP address is in array of
   // IPs that should not be shown adverts.
   //==========================================    
   
   $ipAllowed = true;

   foreach($bannedIPs as $ip)
   {
       if (startsWith($clientIP, $ip))
       {
           $ipAllowed = false;
           break;
       }
   }
   
   //================================================
   // Check if the domain the client has arrived from
   // (referrer domain) is in array of referrer
   // domains that should not be shown adverts.
   //================================================
   
   $referrerAllowed = true;

   $referrerDomain = getDomainFromReferrer($referrer);

   if (in_array($referrerDomain, $bannedReferrerDomains))
   {
       $referrerAllowed = false;
   }

   //=================================================
   // Check if the URI of the current page is allowed
   // to show adverts. Ads should be disabled on pages
   // without content such as login, logout, and admin
   // pages.
   //=================================================
   
   $pageAllowed = true;

   if (in_array($pageURI, $bannedURIs))
   {
       $pageAllowed = false;
   }

   $canShow = !isAdmin() && $ipAllowed && $referrerAllowed && $pageAllowed;

   return $canShow;
}

/*
* Returns true if you this is an admin page, false otherwise
* Add your own implementation according to how your website detects admin mode.
*/

function isAdmin()
{
   return false;
}

/*
* Gets the domain from a URI
* e.g. http://www.facebook.net/ => facebook.net
* https://www.facebook.com/ => facebook.com
*/

function getDomainFromReferrer($ref)
{
   // strip off http / https protocol part
   if (substr($ref, 0, 7) == 'http://')
   {
       $ref = substr($ref, 7);
   }
   else if (substr($ref, 0, 8) == 'https://')
   {
       $ref = substr($ref, 8);
   }

   // count the dots to strip off any subdomains
   $dots = 0;

   for ($i = 0; $i < strlen($ref); $i++)
   {
       $c = $ref[$i];

       if ($c == '.')
       {
           $dots++;
       }
   }

   if ($dots > 1)
   {
       $seenDots = 0;

       for ($i = 0; $i < strlen($ref); $i++)
       {
           $c = $ref[$i];

           if ($c == '.')
           {
               $seenDots++;

               if ($seenDots == $dots - 1)
               {
                   // seen all dots except the one before the TLD
                   // so take the substring to get the domain
                   $ref = substr($ref, $i+1);
                   break;
               }
           }
       }
   }

   // if there is a trailing slash the remove it
   $trailingSlashPos = strpos($ref, '/');

   if ($trailingSlashPos !== false)
   {
       $ref = substr($ref, 0, $trailingSlashPos);
   }

   return $ref;
}

/*
* Returns true the string $haystack begins with the string $needle
*/

function startsWith($haystack, $needle)
{
   $length = strlen($needle);
   return (substr($haystack, 0, $length) === $needle);
}
?>