Results 1 to 4 of 4

Thread: rotating random link

  1. #1
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default rotating random link

    Hello - this is my first post and I am quite new to using PHP, I have nevver made a script of my own but feel competent making amends to open source ones I download.

    I am quite an experienced HTML developer and designer and am working on a project where I have had a request to inset a text link on a page that will change at random between a selection of text links that are in a text file hosted on the server.

    I have decided it will be a good investment to start to learn php and have downloaded a few tutorials and signed up to some forums but for the purpose of this page I wondered if anyone could advise me of a script that might be out there that could look at a text file located at www.randomurl.com/urls.txt and pull in a site at random or in rotation?

    Thanks for taking the time to read

  2. #2
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default

    Here is a simple script you can use. Provided for learning purposes. Place each <a> links on there own lines in the text file. I am sure there's better ways of doing.

    PHP Code:
    <?php
        $adfile 
    "urls.txt";
        
    $ads = array();
        
        
    $fh fopen($adfile"r");
        while(!
    feof($fh)) {
            
    $line fgets($fh10240);
            
    $line trim($line);
            if(
    $line != "") {
                
    $ads[] = $line;
            }
        }

        
    $num count($ads);
        
    $idx rand(0$num-1);
        echo 
    $ads[$idx];
    ?>
    Ryan
    Sevierville, TN

  3. #3
    Join Date
    Oct 2008
    Posts
    42
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default

    Taking the same approach as NXArmada, you can trim the code down a bit.

    PHP Code:
    <?php
        $adfile 
    "urls.txt";
        
        
    $ads file($adfile);
        
    $rand_ad array_rand($ads);
        
        echo 
    $ads[$rand_ad];
    ?>

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Do you want random, or rotating. Both of the above are rotating.
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •