Using PHP to Randomly Generate Adsense Code for your Sidebar

by Teresa Valdez Klein on May 7, 2007

Entrepreneurial bloggers often rely on revenue from Google’s AdSense. But for some bloggers, embedding ads isn’t as simple as pasting a javascript into a sidebar file. When more than one person or company needs to get ad revenue from the same blog, things can get complicated.

One excellent solution is Harley Quinn’s Author AdSense plugin for WordPress. The plugin requires the user to embed a template tag within the WordPress loop. For each post, the plugin randomly enters the AdSense code for the author of the post, or that of the blog’s owner in a pre-determined ratio.

The problem with this plugin is that its template tag relies upon being placed within the WordPress loop. So it’s impossible to place it in the sidebar, or any other location that does not have an author assigned to it.

If you have only two authors to display, a simple if/then statement will accomplish the same thing. Here’s how we recently did it on our blog CS Bloggers.

srand(time()); $random = (rand()%101); if($random <= 50){

Insert first Google AdSense script here.

} else {

Insert second Google Adsesnse script here.

}

Basically, this code asks PHP to generate a random number between 1 and 101. If the number is less than 50, it generates Adsense code for one of the two parties. If the number is greater than 50, it generates code for the other party.

If you would like to divide the revenues among three parties, it can be done as such:

srand(time()); $random = (rand()%101); if($random <= 33){

Insert first Google Adsense script here.

} else {

srand(time()); $random = (rand()%101); if($random <= 50){

Insert second Google Adsense script here

} else {

Insert third Google Adsense script here

}

}

This says select the first code 1/3 of the time, then divide the remaining two thirds up among the other two codes.

Similarly nested if/then statements can be used to divide up any number of codes, although at some point, the syntax might become a bit confusing.

{ 2 comments… read them below or add one }

1 TeresaCentric » Very Cranky Today 05.07.07 at 5:41 pm

[...] thing I’m particularly proud of can be found here. I figured this out all on my own, without any help from anyone. Not too bad for a sick kid with a [...]

2 Jonah 05.09.07 at 7:20 pm

A couple comments from an anal-retentive coder:

1. I’m pretty sure random() % N will produce all values from 0 to N – 1, not from 1 to N – 1 or 1 to N. Modulo computes the remainder of a division, so presumably if the number divides evenly, the remainder will be 0.

2. There’s no reason to compute two random values. All you really need is random() % 3, and if it’s 0 you do the first thing, 1 for the second thing, and 2 for the third thing.

Thus, your final snippet of code could be simplified to:

srand(time()); $random = rand()%3;

if($random == 0){

Insert first Google Adsense script here.

} elseif($random == 1) {

Insert second Google Adsense script here

} else {

Insert third Google Adsense script here

}

You could also use a switch statement if you start needing more than just a couple of different cases.

Sponsored links

advertise here