i am trying to make random redirect using header refresh
in PHP. for some reason i cannot use header location
.
The code placed in example.org
$url = array('https://example.com/','https://example.net/');
shuffle($url);
header("refresh: 0;url=$url");
This redirecting to example.org/Array
instead of the urls
If i use like header("refresh: 0;url=$url[0]");
it redirecting to example.com but i want to make it random.
CodePudding user response:
To select random value from array first you need to select random key from array and then value like this
$url = array('https://example2.co/','https://example33.net/');
$key = array_rand($url);
$new_url = $url[$key];
header("refresh: 0;url=$new_url");
CodePudding user response:
With only two items in the array you're quite likely to get the same thing every time.
You could do something like this with array_rand
$url = array('https://example.com/','https://example.net/');
$key = array_rand($url, 1);
header("refresh: 0; url={$url[$key]}");
CodePudding user response:
$url = array('https://google.com/','https://yahoo.com/');
shuffle($url);
$rand_url = $url[rand()&1];
header("refresh: 0;url=$rand_url");
CodePudding user response:
This works for me
$url = ['https://example.com','https://example.net'];
$size = count($url);
$random=rand(0, $size - 1);
header("refresh: 0;url=$url[$random]");