Ok, I needed to dispaly One banner per-page-view, and start at the beginning when I reached the last banner. (Which will always be no greater than 10.)

The only thing about this was, I couldn't figure out how to store and keep track of page counts without use of a database or a flat file. So I did it flat-file, and there's probably an easier way. While this script works just fine, maybe some one has an an idea that would be easier and faster?

<?php
/* Counting Page Hits to rotate the banners, Store it in a file. */
$store = "track.dat";
if(file_exists($store))
{
	$fp = fopen($store, "r+");
	flock($fp, 1);
	$count = fgets($fp, 4096);
	$count += 1; 
	fseek($fp,0);
	fputs($fp, $count);
	flock($fp, 3);
	fclose($fp);
} 
else { 
 /* If $store cannot be opened, alert some one. */
  print "Err.";
}
/* Calling Banners in Sequence, when we reach the last banner, restet the tracker file */
	$pos = file_get_contents('track.dat');
	$limit = 10;
	$reset = 0;
	if ($pos >= $limit) {
	$fp = fopen($store, "w");
	fputs($fp, $reset);
	fclose($fp);
 }
 echo "$pos \n<br>";
 require_once('track.dat');
 ?>

Test page.

<?php
/* Banner Array */
$advert = array( 
     "0" => "image1", 
     "1" => "image2", 
     "2" => "image3", 
     "3" => "image4", 
     "4" => "image5", 
     "5" => "image6", 
     "6" => "image7",
     "7" => "image8",
     "8" => "image9",
     "9" => "image10"
);
	$pos = file_get_contents('track.dat');
	print $advert[$pos];
?>

    it looks like you found the best method
    i am sure it works
    in this case cant be any issues using a plain 'track.dat' file
    and no need to make any more complicated than this

      why not use a session? or cookie? If you look at cookie and don't find a count var because user has cookies turned off, pick banner at random...doesn't guarantee full rotation but better than nothing.

      The advantage of session is that each visitor would see all 10 banners if they visit 10 pages, whereas w/ the file method 3 people could access the page, each seeing different banner, not necessarily starting w/ #1. And there is always problem of concurrent modification of the file (lock issues).

        Write a Reply...