Web Hosting Talk







View Full Version : Random Mirror Selection Script.


Wilcox
01-26-2006, 06:46 PM
Hello!

I am new to php and am not real sure where to get started on a script I need for my site. I have an idea how I want it to work, but just do not have the skills to do it. Let me tell you what I am going to do with it, I am going to create a Linux download site and the script needs to select from a list of multiple mirrors.

Now here is my idea of how I need this script to work. Lets say that the script is called download.php and the download to the slackware distrobution is slackware_CD_1.iso The URL that I would need to generate is mysite.com/download.php/32/slackware_CD_1.iso

The 32 in the URL is referring to the mirrors that would need to be randomly selected and the last part of the link "slackware_CD_1.iso" is the same on all of the servers so that would not need to be changed. The script would have hundreds of mirrors and CD's to manage so the 32 was just an example.

Now I am not sure how to create this script. I am sure you would have to put the mirrors in an array and use the srand() to randomize the selection.

If someone could help me with this script I would really appreciate it!

Thanks,

Brett Wilcox

realwebsolution
01-26-2006, 07:28 PM
Try this.
Archive Access Multiplexer
Description:
Do you know the great CPAN (Comprehensive Perl Archive Network) under http://www.perl.com/CPAN? This does a redirect to one of several FTP servers around the world which carry a CPAN mirror and is approximately near the location of the requesting client. Actually this can be called an FTP access multiplexing service. While CPAN runs via CGI scripts, how can a similar approach implemented via mod_rewrite?
Solution:
First we notice that from version 3.0.0 mod_rewrite can also use the "ftp:" scheme on redirects. And second, the location approximation can be done by a RewriteMap over the top-level domain of the client. With a tricky chained ruleset we can use this top-level domain as a key to our multiplexing map.
RewriteEngine on
RewriteMap multiplex txt:/path/to/map.cxan
RewriteRule ^/CxAN/(.*) %{REMOTE_HOST}::$1 [C]
RewriteRule ^.+\.([a-zA-Z]+)::(.*)$ ${multiplex:$1|ftp.default.dom}$2 [R,L]
##
## map.cxan -- Multiplexing Map for CxAN
##

de ftp://ftp.cxan.de/CxAN/
uk ftp://ftp.cxan.uk/CxAN/
com ftp://ftp.cxan.com/CxAN/
:
##EOF##
More details here: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Wilcox
01-26-2006, 07:38 PM
Thanks,

I have a perl script to do exactly that, the only problem is that I have to create so many files and I was trying to simplify the proccess using php.

Thanks for the link though...

jstanden
01-27-2006, 06:43 AM
If you're looking for a quick "back-of-the-envelope" example, here you go:


<?PHP
srand();

$baseURL = "http://www.mysite.com/download.php";
$fileName = "slackware_CD_1.iso";

// Mirror options
$mirrors = array(
"mirror1",
"mirror2",
"mirror3",
"mirror4",
"mirror5"
);

// Choose a random mirror element from the array
$index = rand(1,count($mirrors)) - 1;

// Output URL
echo sprintf("%s/%s/%s",
$baseURL,
$mirrors[$index],
$fileName
);