给个php的例子吧,你自己根据这个修改下
1.创建一个名为 urls.txt 的文件,每行存放一个媒体URL
2.创建 urls_ordered.txt 文件,如果不存在,则从 urls.txt 读取并打乱顺序存入,创建 pointer.txt 文件,记录当前读取的位置,初始值为0
[PHP] 纯文本查看 复制代码 <?php
$lock = fopen('lock.txt', 'w');
if (!flock($lock, LOCK_EX)) {
die('无法获得锁');
}
$ordered_urls = file('urls_ordered.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($ordered_urls === false) {
$urls = file('urls.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($urls === false) {
die('无法读取URL文件');
}
shuffle($urls);
file_put_contents('urls_ordered.txt', implode("\n", $urls));
$ordered_urls = $urls;
}
$pointer = file_get_contents('pointer.txt');
if ($pointer === false) {
$pointer = 0;
} else {
$pointer = intval($pointer);
}
$current_url = $ordered_urls[$pointer];
$pointer++;
file_put_contents('pointer.txt', $pointer);
if ($pointer >= count($ordered_urls)) {
$pointer = 0;
shuffle($ordered_urls);
file_put_contents('urls_ordered.txt', implode("\n", $ordered_urls));
file_put_contents('pointer.txt', $pointer);
}
header('Location: ' . $current_url);
exit;
flock($lock, LOCK_UN);
fclose($lock);
?>
在每次访问都会随机给一个URL地址 |