SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member
    Join Date
    Nov 2005
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple PHP Random Script

    My site has a lot of popular video downloads which use to make my site reach its 40Gb a day bandwidth limit fairly quickly. This made my whole site go down and was pretty crappy. So, since then I've purchased another 40Gb a day hosting account so I can host the videos on a sperate account so if that account reaches its bandwidth limit, atleast my site will still be up.

    Well, now I need another 40Gb account. This will require some kind random script which will randomly select a download server out of two possible servers/accounts. So I was wondering what would be the easiest way to do this and could someone write me a simple script for it.

    I'll probably get the domain www.crysis-online-files-2.com, so the two possible domains would be the one above and www,crysis-online-files.com. Would it be best to randomly select a server when a user clicks the download link, or would it be better to output different links to the page everytime it loads?

    Can someone help?

  2. #2
    Counter-Striker
    Join Date
    Sep 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I dont know much but if you have the same files on all 3 servers you could do a php random script like
    PHP Code:
    $number rand(1,2);
    $one[1] = "http://www.crysis-online-files.com";
    $one[2] = "http://www.crysis-online-files-2.com";
    echo 
    $one[$number]; 
    It would be easier to select it when the page loads.

  3. #3
    SitePoint Enthusiast Salkcin's Avatar
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess the esiest thing would be to make a small function that takes care of the "server selection". Then when you display your available downloads, you run the function and it randomize a download location for you. Since my english is not so good, i'll give you an example instead (untested!!).

    PHP Code:
    <?php

    // Function to select a server by random
    function SelectServer() {
    $server = array("http://www.server1.com""http://www.server1.com""http://www.server1.com");
    shuffle($server);

    return 
    $server[0];
    }

    include(
    "your_db_connection.php");

    $sql "SELECT * FROM your db_table";
    $result mysql_query($sql);

    while (
    $row mysql_fetch_assoc($result)) {

        
    // Get a downloadlocation from the SelectServer() function
        
    $download_location SelectServer();
        
        
    // ... and the download link is...
        
    echo "<a href=\"$download_location/$row[filename]\">$row[description]</a><br />\n";
        
    }

    ?>

  4. #4
    Evil Genius MilchstrabeStern's Avatar
    Join Date
    Nov 2003
    Location
    Arizona
    Posts
    1,131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, you're probably better off using shuffle, just makes your code a little smaller.

    But both the solutions above should work for you. Curious, who's your host?
    ]

  5. #5
    Non-Member
    Join Date
    Nov 2005
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the scripts

    Servage.net, they're an excellent host, really solid.

    If you're going to sign up (or anyone else), use this coupon CUST17321 for some more disk space and I'll get a $12 commission or something.

    Cheers!

  6. #6
    Evil Genius MilchstrabeStern's Avatar
    Join Date
    Nov 2003
    Location
    Arizona
    Posts
    1,131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, ok.

    I think my host provides 1,500 gb/month for $4, so I'll stick with them for now.
    ]

  7. #7
    SitePoint Enthusiast Salkcin's Avatar
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My host gives unlimited transfer


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
  •