Posted on 26 Jul, 2006 by Paul
NOTE: This is only here for historical purposes. The script has since been updated and the latest version is available from:
PHP Adsense-Referrals Script Version 2.1
I’ve written a simple PHP script to randomise which Google Adsense referral is displayed (firefox, picasa, adwords or adsense) and to easily handle publisher ID and channel for them. Additionally, the Firefox referral is only displayed if a browser other than Firefox is being used.
The script is easy to configure: just copy the code below into a text editor and change $adsense_pub_id and $adsense_referral_channel to your publisher ID (essential) and referral channel (optional) respectively. Save the file as adsense.inc.php (for example) on your webserver (it’s a good idea to keep it outside of the document root, i.e. store it in /home/mydomain.com rather than /home/mydomain.com/public_html).
<?php
// Copyright 2006 Paul (paul at tiglo.co.uk - www.tiglo.co.uk)
// Free to use but please keep the copyright in tact
$adsense_pub_id = "ca-test";
$adsense_referral_channel = "xxxxxx";
function g_referral() {
if(preg_match("/firefox/i", $_SERVER['HTTP_USER_AGENT'])) {
$referral = rand(2,4);
} else {
$referral = rand(1,4);
}
if($referral == 1) { ff_referral(); }
if($referral == 2) { adsense_referral(); }
if($referral == 3) { adwords_referral(); }
if($referral == 4) { picasa_referral(); }
}
function ff_referral() {
global $adsense_pub_id;
global $adsense_referral_channel;
?>
<div class="adsense">
<!-- Begin Firefox referral code -->
<script type="text/javascript"><!--
google_ad_client = "<?php print($adsense_pub_id); ?>";
google_ad_width = 125;
google_ad_height = 125;
google_ad_format = "125x125_as_rimg";
google_cpa_choice = "CAAQyf6XhAIaCI9aHswlZ6VKKK2293M";
google_ad_channel = "<?php print($adsense_referral_channel); ?>";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<!-- End Firefox referral code -->
<?php
}
function adsense_referral() {
global $adsense_pub_id;
global $adsense_referral_channel;
?>
<div class="adsense">
<!-- Begin Adsense referral code -->
<script type="text/javascript"><!--
google_ad_client = "<?php print($adsense_pub_id); ?>";
google_ad_width = 125;
google_ad_height = 125;
google_ad_format = "125x125_as_rimg";
google_cpa_choice = "CAAQ1d-WhAIaCG76IbO_XdEoKJ_D93M";
google_ad_channel = "<?php print($adsense_referral_channel); ?>";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<!-- End Adsense referral code -->
</div>
<?php
}
function adwords_referral() {
global $adsense_pub_id;
global $adsense_referral_channel;
?>
<div class="adsense">
<!-- Begin Adwords referral code -->
<script type="text/javascript"><!--
google_ad_client = "<?php print($adsense_pub_id); ?>";
google_ad_width = 125;
google_ad_height = 125;
google_ad_format = "125x125_as_rimg";
google_cpa_choice = "CAAQ4PqkgwIaCKjw_7VSwBCiKMzZ6n4";
google_ad_channel = "<?php print($adsense_referral_channel); ?>";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<!-- End Adwords referral code -->
</div>
<?php
}
function picasa_referral() {
global $adsense_pub_id;
global $adsense_referral_channel;
?>
<div class="adsense">
<!-- Begin Picasa referral code -->
<script type="text/javascript"><!--
google_ad_client = "<?php print($adsense_pub_id); ?>";
google_ad_width = 125;
google_ad_height = 125;
google_ad_format = "125x125_as_rimg";
google_cpa_choice = "CAAQ5PKkgwIaCDtq5r7WVtRoKOjKrIMB";
google_ad_channel = "<?php print($adsense_referral_channel); ?>";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<!-- End Picasa referral code -->
</div>
<?php
}
?>
After configuring and saving the script, edit your pages to include the following code (once):
<?php include "/path/to/file/adsense.inc.php"; ?>Replace /path/to/file with the directory path to the script (e.g. /home/mydomain.com) and ensure the / is present before adsense.inc.php (or whichever filename you prefer to give the script). The referral code can now be called by placing the following code within your php file:
<?php g_referral(); ?>But only one occurrence as displaying a referral of the same type more than once is against Google Adsense’s Policies [1]. It is possible for course to call a specific referral, if you’re wanting to display more than one per page (but they won’t be randomised, I might add this later), by using the following code:
<?php picasa_referral(); ?>For displaying adsense, adwords or a firefox referral, replace picasa with the referral type you want to offer.
If you want to use different Adsense Referral formats, replace the code that’s between <!– Begin <product> referral code –> and <!– End <product> referral code –>. Replace your publisher ID with <?php print($adsense_pub_id); ?>, then save the file and the new formats will be loaded instead. (It’s especially helpful to use a variable in place of the actual adsense ID so that switching to test mode can be done easily and quickly - just replaced $adsense_pub_id = "pub-xxxx"; with $adsense_pub_id = "ca-test";.
The script is designed to work within php pages. However, it’s also possible to use php within static HTML pages by editing the apache (webserver) configuration to process .html/.htm pages as php, too. The config file (httpd.conf, or possibly you need to edit a separate php4.conf file instead) is usually found in /etc/httpd/ (or /etc/apache/, /etc/apache2/, /etc/apache2/conf.d/). Look for an entry similar to:
<IfModule mod_php4.c> AddType application/x-httpd-php .inc .php .php4 .php3 AddType application/x-httpd-php-source .phps </IfModule>Append .html (and/or .htm .phtml) to the line "AddType application/x-httpd-php .inc .php .php4 .php3". In the case of apache2, the module entry will be "sapi_apache2.c".
Now restart apache and the referral ads should now be randomly displayed on the pages where the code was inserted. Note: If you are using shared hosting for your sites you should contact your webhosting company to ask for processing php with .html pages to be enabled (if it’s not already enabled) or change your pages’ file extension to .php instead of .html.
[1] https://www.google.com/support/adsense/bin/answer.py?answer=25892&topic=1449
Category: Adsense | Comments: 1









Write a comment: