首页 > 解决方案 > 使用 PHP 随机重定向

问题描述

我正在尝试随机重定向 2 个 URL,但它不起作用。

我的代码

<?php
$urls=readStack();
if(empty($urls)) {
    $urls = shuffle(array('https://domain1.com', 'https://domain2.com')); 
    $url=array_pop($urls);
    storeStack($urls);
    header('Location:' .$url); 
?>

帮助如何解决这个问题?

标签: phpredirect

解决方案


我不知道是什么readStack()storeStack()但你可能想要类似的东西

$arr = array('https://domain1.com', 'https://domain2.com');
shuffle($arr); // This returns a boolean and the array is modified inside the function so just pass in the array
$url=array_pop($arr); // Pop the top most element from the array and store into $url
header("Location: $url");

仅此代码即可为您提供这两个中的随机网址


推荐阅读