我需要自动刷新页面,以便机器人读取它,每次显示的文本都会有所不同。我有这个,但我需要知道我是否可以改进它?
<?php
$deaths = array("with a potato.", "by using a truck and running them over.", "with a gun, bang bang.", "by stuffing a mouldy lemon in their mouth.", "by throwing multiple knives at them.", "by tying them up and forgetting about them.", "by throwing them off a building onto a cactus.");
$death=array_rand($deaths);
echo $deaths[$death];
$url1=$_SERVER['REQUEST_URI'];
header("Refresh: 0.5; URL=$url1");
?>
为什么不直接使用 header("Refresh:0");
?
- 选择
服务器端刷新的事实是一个不错的选择,因为它比客户端刷新更快,例如使用 JavaScript (
window.location.reload()
) 或 HTML (<meta http-equiv="refresh" content="5" />
)。有关此主题的更多信息,我建议您阅读这篇有趣的文章([客户端与服务器端渲染])您为机器人设计了这个,那么为什么要在刷新之前等待呢?
如果您的页面中有其他 PHP 脚本,这些脚本不得由客户端浏览器缓存或服务器和客户端浏览器之间的任何代理缓存,则可以将以下行添加到您的页面:
header("Cache-Control: no-cache, must-revalidate");
另外,正如您的问题评论中提到的:
必须在发送任何实际输出之前调用 header() 普通 HTML 标签,
- 由于刷新相同的 URI,因此不要给服务器太多工作。我的意思是,删除此指令:
$url1=$_SERVER['REQUEST_URI'];
鉴于这些事实,我建议按如下方式更改您的代码:
<?php
header("Cache-Control: no-cache, must-revalidate"); //if 3rd point is satisfied
header("Refresh: 0");
$deaths = array("with a potato.", "by using a truck and running them over.", "with a gun, bang bang.", "by stuffing a mouldy lemon in their mouth.", "by throwing multiple knives at them.", "by tying them up and forgetting about them.", "by throwing them off a building onto a cactus.");
$death=array_rand($deaths);
echo $deaths[$death];
?>