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.
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");
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]}");
You can create an array of URLs & just shuffle between those to pick at random. Then once one is picked you can header refresh to that URL:
$url = array('https://google.com/','https://yahoo.com/');
shuffle($url);
$rand_url = $url[rand()&1];
header("refresh: 0;url=$rand_url");
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]");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.