Check For Localhost From Smarty To Disable Adsense When Testing

May
18

I like to disable adsense in my development environment.  This has one definite benefit (I don't erroneously click on an adsense ad, thereby marking my adsense account as a potential click fraud perpetrator) and one possible benefit (I believe that sites with higher click-through rates get served higher-CPC ads by google).

Since on one site I am using smarty php templates, I had to add a few lines of code to my main index.php file:

$smarty->assign('phpservername', $_SERVER['SERVER_NAME']) ;

(that should be set just before the call to $smarty->display() )

Then additionally I added an additional {if} clause to the file where my adsense banner is normally diplayed, comparing the server name to the string 'localhost' which is the hostname most of us have in our development environments.  Notice also that I've added an else clause with a <div> the same size as the adsense to keep the layout consistent.  Have fun: this isn't hard.  Then (if you use tracking software like google analytics or piwik) you probably want to put the same {if} code around your tracking javascript.

 

{if $phpservername != 'localhost' }
{literal}
<script type="text/javascript"><!--
google_ad_client = "pub-2724380293080134";
/* 728x90, CF home page center banner - created 4/28/09 */
google_ad_slot = "8568021993";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
{/literal}
{else}
<div style="background-color:#999; height: 90px ; width: 728px ; "> google ad </div>
{/if}

 

By they way, whenever you're including javascript in a smarty template, it's a good idea to enclose your code in {literal} tags -- while not necessarily vital every time you include javascript, it is definitely important if you have any curly-brackets { or } in your javascript, which tend to crop up in anything but the most mundane javascript.  Keep it safe: use {literal} all the time unless you have a good reason not to.