How to avoid showing more than 3 google adsense content units on a page

Google adsense is being used by publishers to show advertisements on their webpages, websites and web properties. Due to dynamic nature of various web pages some times it happens that more than maximum number of ads are shown than allowed. Like in case of Google Adsense it is 3 and it may happen that you have put the adsense code 4 times on a page like 1 in header section, other in content section, another in sidebar and 4th one in footer. And this may happen when you have common header, sidebar and footer i.e. 3 ads are running on every page and you have inserted one more ad in content, making the total count 4.

So, to avoid such kind of situation use simple PHP code to control it. In your main settings file create a variable say,

[php]$ad_max_allowed=3;[/php]

and initialise ad showing counter to 0

[php]$ad_shown_count=0;[/php]

Now, where you show an ad, increment this counter and check if that count has exceeded more than the allowed number of ads.

[php]
if($ad_shown_count<$ad_max_allowed)
{
$ad_shown_count++;
//display ad here
}
[/php]

With this implementation, only maximum number of ads will be shown.

Now, tweaking this code further for a dynamic website. Lets say on some pages we want to display ads in header, content and sidebar and on other page we want to show ads on header, sidebar and footer only.
To achieve this, define slots in database and in your pages. First create a page table where page information is stored. In this table create a column say, ad_slots and write a string like 1110 which means slot 1, slot 2 and slot 3 are activated for this particular page and for the second page it could be 1011 which means slot 1, slot3 and slot 4 are activated. Here,

  • Slot 1 – Header
  • Slot 2 – Content
  • Slot 3 – Sidebar
  • Slot 4 – Footer

This could be changed and made more advanced as per the requirement.

Now, when a page is being rendered just check before the adsense code if this ad slot has to be shown on this page or not.

[php]
$ad_slot_number=2; //Slot number defined for this slot.
if($ad_slot_array[$ad_slot_number]==’1′)
{
if($ad_shown_count<$ad_max_allowed)
{
$ad_shown_count++;
display ad here
}
}
[/php]

Here, $ad_slot_number is the ad slot number for the page. And $ad_slot_array is the array formed from the ad_slot column from page table where ad_slot string is converted to array.

Now, it is checked if ad_slot_number in $ad_slot_array value is 1. If it is 1 then show the ad.

While implementing this technique make sure that spacing, segments, DIV and TABLE are being displayed correctly.

Leave a Comment