从网上搜了下,基本都是 ran 随机输出一条数据,刷新就会变化。
怎么实现,随机后固定一条?
假如,txt 里 整理了 10条 图片的 URL 链接,
http://localhost/1.jpg
http://localhost/2.jpg
.................
http://localhost/10.jpg
网站前台内容页调用这个 随机并固定输出一条的代码,
访问 http://localhost/show_112.html 内容页,
每个内容页都会调用这个随机并固定输出的代码,
访问后,输出 一条 图片的 URL http://localhost/2.jpg,
那么刷新
http://localhost/show_112.html 这个页面,
输出的都是 http://localhost/2.jpg 这一条。
需要实现 每个内容页 输出的一张照片不重复,且刷新页面后,每个页面的图片都是固定死的一条不变。
这是我现在的代码,应该如何改?
public static function suiji_list($vars) {
// 存储数据的文件
$filename = 'test.txt';
if(!file_exists($filename)) {
die($filename . ' 数据文件不存在');
}
// 读取整个数据文件
$data = file_get_contents($filename);
// 按换行符分割成数组
$data = explode(PHP_EOL, $data);
// 随机获取一行索引
$result = $data[array_rand($data)];
// 去除多余的换行符(解决获取空值问题
$result = str_replace(array("\r","\n","\r\n"), '', $result);
$url = 'http://localhost'.$result.'';
echo $url;
//header("Location:".$url);
}
随机也可以啊,每个页面固定随机种子即可