我正在嘗試header refresh在 PHP 中使用隨機重定向。由于某種原因,我無法使用header location.
放入的代碼example.org
$url = array('https://example.com/','https://example.net/');
shuffle($url);
header("refresh: 0;url=$url");
這重定向到example.org/Array而不是網址
如果我使用喜歡header("refresh: 0;url=$url[0]");它重定向到 example.com 但我想讓它隨機。
uj5u.com熱心網友回復:
要首先從陣列中選擇隨機值,您需要從陣列中選擇隨機鍵,然后像這樣的值
$url = array('https://example2.co/','https://example33.net/');
$key = array_rand($url);
$new_url = $url[$key];
header("refresh: 0;url=$new_url");
uj5u.com熱心網友回復:
陣列中只有兩個專案,您很可能每次都得到相同的東西。
你可以做這樣的事情array_rand
$url = array('https://example.com/','https://example.net/');
$key = array_rand($url, 1);
header("refresh: 0; url={$url[$key]}");
uj5u.com熱心網友回復:
$url = array('https://google.com/','https://yahoo.com/');
shuffle($url);
$rand_url = $url[rand()&1];
header("refresh: 0;url=$rand_url");
uj5u.com熱心網友回復:
這對我有用
$url = ['https://example.com','https://example.net'];
$size = count($url);
$random=rand(0, $size - 1);
header("refresh: 0;url=$url[$random]");
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/480191.html
上一篇:如何使用RetrieveAPIView根據DjangoRestFramework中的特定欄位檢索資料?
下一篇:特定的htacces重定向例外
