View Full Version : code needed to auto forward randomly to 1 of 2 sites!
zoldos
04-03-2016, 08:04 AM
This is my current code:
<?php
{ header("Location: /ts"); exit(); }
?>
I'd like to change it, or make some new code, so it will randomly forward either to the above site, or another on the same domain (50% chance of either site). I have very little PHP experience. Thanks!! :D
Beverleyh
04-03-2016, 08:13 AM
Look into array_rand() http://php.net/manual/en/function.array-rand.php
There are code examples in the link above and here http://stackoverflow.com/a/8742221
You will also find many other code examples via Google if you search something like "php random select from 2 strings"
zoldos
04-03-2016, 08:55 AM
Okay great! Here's what I have now:
<?php
$a=array("/ts","/ts2");
$random_keys=array_rand($a,2);
if ($a='/ts') {
header("Location: /ts"); exit();
} elseif ($a='/ts2') {
header("Location: /ts2"); exit();
}
?>
I ran it about 10 times and it always goes to "/ts". Can you offer some suggestions? Thanks!
jscheuer1
04-03-2016, 03:01 PM
Though you're dancing all around it, there's a lot wrong there. $a isn't changed by array_rand, so it's still an array, but then if ($a='/ts') sets it (to test it you need two equal signs == just one = sets it) to /ts in a "truthy" fashion (all setting of non false values resolves as true to the boolean parser), so header("Location: /ts"); exit(); will always happen.
You can actually work this out with array_rand, but as I was looking at the man page, it was suggested there was a better method, mt_rand (said to be faster and closer to truly random). It works a little differently. In any case, this does the trick:
<?php
$a = array("/ts", "/ts2");
header("Location: " . $a[mt_rand(0, count($a) - 1)]);
die();
?>
Now random is random though, and you only have two choices, so sometimes you will get the same result a few times or more in a row. Ten is unlikely, but could happen. Even 100, though that's very unlikely. In fact, after just 20 or so tries, you should be seeing it go to one about half the time, and the other the rest of the time. I tried both methods (array_rand and mt_rand), and the mt_rand method seemed more evenly distributed.
zoldos
04-03-2016, 05:07 PM
Though you're dancing all around it, there's a lot wrong there. $a isn't changed by array_rand, so it's still an array, but then if ($a='/ts') sets it (to test it you need two equal signs == just one = sets it) to /ts in a "truthy" fashion (all setting of non false values resolves as true to the boolean parser), so header("Location: /ts"); exit(); will always happen.
You can actually work this out with array_rand, but as I was looking at the man page, it was suggested there was a better method, mt_rand (said to be faster and closer to truly random). It works a little differently. In any case, this does the trick:
<?php
$a = array("/ts", "/ts2");
header("Location: " . $a[mt_rand(0, count($a) - 1)]);
die();
?>
Now random is random though, and you only have two choices, so sometimes you will get the same result a few times or more in a row. Ten is unlikely, but could happen. Even 100, though that's very unlikely. In fact, after just 20 or so tries, you should be seeing it go to one about half the time, and the other the rest of the time. I tried both methods (array_rand and mt_rand), and the mt_rand method seemed more evenly distributed.
I really need to learn more about PHP. I used to code in C++ back in the early to mid 90's when I ran a BBS and it seems similar.
Anyway, thank you so much, it works great! I saw it switching between sub-folders/sites perfectly! :D
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.