繁体   English   中英

使用PHP自动刷新网页

[英]Using PHP to auto refresh webpage

我需要自动刷新页面,以便机器人阅读它,每次显示的文本都会不同。 我有这个,但我需要知道是否可以改进?

<?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");

  • 另外,正如您的问题评论中提到的那样:

在发送任何实际输出之前,必须先通过常规HTML标签调用header(),

  • 由于您刷新相同的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];
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM