php - 使用 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);
?>
帮助如何解决这个问题?
解决方案
我不知道是什么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");
仅此代码即可为您提供这两个中的随机网址
推荐阅读
- java - 是否可以在 Firestore 中的对象数组中查询对象字段?
- vba - 创建 vba 代码以连接到远程 SQL 服务器,执行更新 accdb 文件上的表的存储过程
- html - 我可以在谷歌图表中将 x 轴和 y 轴都设为字符串吗?
- python-3.x - scrapinghub上传和使用文件
- npm - @ngtools/webpack:检测到 Angular 编译器,但它是错误类的实例。
- go - 在其他包中使用 main.go 中声明的结构
- ios - 如何根据内容更改 UITableViewCell 大小
- virtual-machine - 虚拟 Windows 10 机器的 VMware Fusion for Mac 没有互联网
- javascript - 如何处理在 ReactJS 中映射为组件的一组数据的排序?
- performance - 淘汰赛嵌套 Foreach 绑定花费了太多时间