Choosing Banner Ads PHP

Another use for random numbers is choosing from banner ads. Suppose you've signed up three sponsors for your Web site. Each has a single banner you promise to display on an equal proportion of hits to your site. To accomplish this, generate a random number and match each number to a particular banner. It is used a switch statement on a call to mt_rand. In a situation like this, you don't need to worry too much about using good seeds. You simply want a reasonable distribution of the three choices. Someone guessing which banner will display at midnight poses no security risk.

Generating a Session Identifier

Generating a Session Identifier

Random Banner Ad

//Seed the generator
mt_srand(doubleval(microtime()) * 100000000);
//choose banner
case 1:
$bannerURL =
$bannerImage = "leon.jpg";
case 2:
$bannerImage = "php_lang.jpg";
$bannerImage = "phptr.jpg";
//display banner
print("<A HREF="$bannerURL">");
print("<IMG SRC= "$bannerImage" ");
print("WIDTH="400" HEIGHT="148" BORDER="0"

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics