PHP Code

Rotate Images Near Adsense Ads with PHP

It is against the Adsense TOS to draw attention to the ads in any inappropriate way. However, many people have found that placing images in the near vicinity can increase CTR. However, how do you keep those images fresh?

This code will create image rotation which you can use to rotate images beside adsense ads. That way they never get stale. You could also use it for some ads rotation if you're not using adense or otherwise.


<?php
    $path_to_images = "images/";  // this is path to your images
    $default_img = "box.gif";  // default image to display if directory listing fails

    function getRandomImage($path, $img) {
       if ( $list = getImagesList($path) ) {
           mt_srand( (double)microtime() * 1000000 );
           $num = array_rand($list);
           $img = $list&#91;$num&#93;;
       }    return $path . $img;
    }

    function getImagesList($path) {
       $ctr = 0;
       if ( $img_dir = @opendir($path) ) {
           while ( false !== ($img_file = readdir($img_dir)) ) {
               // can add checks for other image file types here
               if ( preg_match("/(\.gif|\.jpg)$/", $img_file) ) {
                   $images&#91;$ctr&#93; = $img_file;
                   $ctr++;
               }
           }
           closedir($img_dir);
           return $images;
       }    return false;
    }
?>
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image