Link Rotation Based on Visits

Discussion in 'Programming' started by Wildhalf, Oct 21, 2007.

  1. #1
    Hi All,

    I am looking for the code to rotate a link on my site based on visits. Basically all i need is to display one link when the i get a hit and a different one when i get the second hit and then back to the first one on the third hit and so on. So in other wors a rotation script based on page hits.

    Can anyone help me out??
     
    Wildhalf, Oct 21, 2007 IP
  2. Wildhalf

    Wildhalf Peon

    Messages:
    202
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Got sick of trying to do this with javascript and textboxes I couldn't figure it out at all. So in the end i went back to what i knew and did it with PHP. Took me all of 2 minnutes...

    Create a file on your server called ad.txt

    and create the following php file....

    <?php
    // Open the file and erase the contents if any
    $fh = fopen("ad.txt", 'r');
    // Read the data from the file
    $url_no = fread($fh, 1);
    // Close the file
    fclose($fh);
    //echo $url_no;
    
    if ($url_no == 1){
    echo "http://www.e-z-m.com";
    $your_data = "2";
    }
    elseif ($url_no ==2) {
    echo "http://www.ebooks4u.eu";
    $your_data = "1";
    }
    
    // Open the file and erase the contents if any
    $fp = fopen("ad.txt", "w");
    
    // Write the data to the file
    fwrite($fp, $your_data);
    
    // Close the file
    fclose($fp);
    
    ?>
    PHP:
    The code reads in a value from a text file 1 or 2 and from there changes the URL displayed. Then it deletes the contents of the text file and writes the opposite number to it. So every time the site is visited it changes the value in the text file from a 1 to a 2 or a 2 to a 1.

    You could easily add more URLs to the rotator by adding more elseif statements.
     
    Wildhalf, Oct 22, 2007 IP