Well... I run a theater as part of my site that hosts independant films, and we're going to be hosting a film soon that will get thousands of downloads ("Ideality", you may have heard of it if you're into CG/filmmaking/etc.), and it'll be a big file.
As such, I decided we needed mirrors. But then I thought about the downsides.
Really... who clicks the mirror links? No one... they want to download the "real file" from the "real host"... it doesn't really save much bandwidth for the "real host".
So... the obvious option then is to only give them one option to click... a random link from the list of mirrors...
Then I realized that didn't quite work because all they'd have to do is refresh, and they might download the file twice accidentally or something, making it better, but, again, not a great solution.
So... here's a perfect answer to this:
I'll write this in a tutorial style, so you can follow along with how to add it to the page, rather than just an example:
Put the following php at the top of your page:Now you've got your random number setup. It's almost totally random which number an IP will get. The only way it can change is if the IP changes... so, basically, you're guaranteed, at least for a single visit, that that person will only get one number.PHP Code:<?php
$ip = $_SERVER['REMOTE_ADDR']; //grabs IP address
$md5 = md5($ip); //randomizes it a bit, to be sure similar IPs are mixed up
$char = $md5[31]; //grabs the last digit of the md5 string
//(yes, it's 32 long, but since it starts at 0, the 32nd number is really 31)
$ascii = ord($char); //turn the character into it's ascii code. Ex: "c" = 99.
$n = $ascii % 10; //the % (mobius) gets the remainder from division.
//this will give you the last digit of a number. Ex: (from above) 99/10= 9r9, so... $n=9.
...
?>
Note: the number will be a single digit, from 0-9.
After this, you need to setup a set of ifs (or a switch statement), like this:Then you've got your mirror list setup.PHP Code:<?php
...
if ($n == 0) $url = "http:/...URL0";
if ($n == 1) $url = "http:/...URL1";
if ($n == 2) $url = "http:/...URL2";
if ($n == 3) $url = "http:/...URL3";
if ($n == 4) $url = "http:/...URL4";
if ($n == 5) $url = "http:/...URL5";
if ($n == 6) $url = "http:/...URL6";
if ($n == 7) $url = "http:/...URL7";
if ($n == 8) $url = "http:/...URL8";
if ($n == 9) $url = "http:/...URL9";
...
?>
Just do your page as is, then do this:
PHP Code:<!--HTML ABOVE THIS... etc... -->
<a href="<?php echo $url; ?>">This is the mirrored link!</a>
<!--HTML below this... and no one is the wiser... looks like the "real" link... -->
There are probably others uses than just mirroring, but most of them probably relate to sorting in some way.
You could have it relate to another number than 10 URLs, too, btw... just assign multiple numbers to the same URL, or divide by 100 with the % command, and you'll have 0-99 as possible outputs, and that will be enough
Here's a working example.... at the moment, it just does the first part of the code, outputting the pseudorandom number that's generated.
http://ci-pro.com/misc/phptest/ipnum.php
Anyway, just thought this could be helpful to people.
Thoughts?
For fun, post your number... just out of curiosity
I'm 9, like I used in the example.
btw, just a note-- I'm not logging IPs or anything if you're worried... its the exact code you see above.




Reply With Quote


Bookmarks