PHP Adsense-Referrals Script Version 2.1


There were some bugs, so I’m released version 2.1.

The bugs were with handling of global variables, which worked fine for one site but not another (and both sites were running on the same server, therefore the same versions of php and apache), so after a lot of messing around and frustration, I finally read a bit deeper in to usage of the global variable and found a helpful (albeit undocumented) tip.

Anyway, for installation and usage instructions, check the following articles:

A PHP Google Adsense Referrals Script
A PHP Google Adsense Referrals Script Version 2
<?php
// Copyright 2006 Paul (paul at tiglo.co.uk - www.tiglo.co.uk)
// Free to use but please keep the copyright in tact

global $adsense;
$adsense['pub_id'] = "ca-test";
$adsense['referral_channel'] = "xxxx";
$adsense_referral = array('firefox' => 0, 'adsense' => 0, 
'adwords' => 0, 'picasa' => 0);

function g_referral() {
  global $adsense_referral;
  
  $using_firefox = preg_match("/firefox/i", $_SERVER['HTTP_USER_AGENT']);
  $referrals = 0;
  
  while($referrals < 1) {
    if(($adsense_referral['firefox'] == 1 OR $using_firefox == 1) AND
        $adsense_referral['adsense'] == 1
	AND $adsense_referral['adwords'] == 1
	AND $adsense_referral['picasa'] == 1) {
      exit;
    }
    if($using_firefox == 1) {
      $referral = rand(2,4);
    } else {
      $referral = rand(1,4);
    }
    if($referral == 1 AND $adsense_referral['firefox'] == 0
       AND $using_firefox == 0) {
      firefox_referral();
      $adsense_referral['firefox'] = 1;
      $referrals = $referrals + 1;
    } elseif($referral == 2 AND $adsense_referral['adsense'] == 0) {
      adsense_referral();
      $adsense_referral['adsense'] = 1;
      $referrals = $referrals + 1;
    } elseif($referral == 3 AND $adsense_referral['adwords'] == 0) {
      adwords_referral();
      $adsense_referral['adwords'] = 1;
      $referrals = $referrals + 1;
    } elseif($referral == 4 AND $adsense_referral['picasa'] == 0) {
      picasa_referral();
      $adsense_referral['picasa'] = 1;
      $referrals = $referrals + 1;
    }
  }
}

function firefox_referral() {
  global $adsense;
?>
<-- Begin Referral Code -->
<script type="text/javascript"><!--
google_ad_client = "<?php print($adsense['pub_id']); 
?>";
google_ad_width = 120;
google_ad_height = 60;
google_ad_format = "120x60_as_rimg";
google_cpa_choice = "CAAQrc6TlwIaCAiFrvJfRIvdKPm093M";
google_ad_channel = "<?php print($adsense['referral_channel']); 
?>";
//--></script>
<script type="text/javascript" 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<-- End Referral Code -->
<?php
}

function adsense_referral() {
  global $adsense;
?>
<-- Begin Referral Code -->
<script type="text/javascript"><!--
google_ad_client = "<?php print($adsense['pub_id']); 
?>";
google_ad_width = 120;
google_ad_height = 60;
google_ad_format = "120x60_as_rimg";
google_cpa_choice = "CAAQ-dOWhAIaCDhxzMnYataRKIHD93M";
google_ad_channel = "<?php print($adsense['referral_channel']); 
?>";
//--></script>
<script type="text/javascript" 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<-- End Referral Code -->
<?php
}

function adwords_referral() {
  global $adsense;
?>
<-- Begin Referral Code -->
<script type="text/javascript"><!--
google_ad_client = "<?php print($adsense['pub_id']); 
?>";
google_ad_width = 120;
google_ad_height = 60;
google_ad_format = "120x60_as_rimg";
google_cpa_choice = "CAAQ-NmkgwIaCE0IWWanjWPsKKTP6n4";
google_ad_channel = "<?php print($adsense['referral_channel']); 
?>";
//--></script>
<script type="text/javascript" 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<-- End Referral Code -->
<?php
}

function picasa_referral() {
  global $adsense;
?>
<-- Begin Referral Code -->
<script type="text/javascript"><!--
google_ad_client = "<?php print($adsense['pub_id']); 
?>";
google_ad_width = 120;
google_ad_height = 60;
google_ad_format = "120x60_as_rimg";
google_cpa_choice = "CAAQtNGIlAIaCB5Js1fhN80UKJzErYMB";
google_ad_channel = "<?php print($adsense['referral_channel']); 
?>";
//--></script>
<script type="text/javascript" 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<-- End Referral Code -->
<?php
}
?>
I will soon start working on version 2.2 (or 3.0) and add support for Google Adsense Ads and a "safe mode" for when viewing your own site (i.e. the script will switch to ca-test instead of your own pub-id).

Share and Enjoy:
  • Digg
  • del.icio.us
  • BlinkList
  • Furl
  • Netscape
  • Reddit
  • StumbleUpon
  • YahooMyWeb




Write a comment:

(Required, but won't be displayed)