如何检测页面何时使用PHP更新

S17*_*514 0 php scripting

我想知道如何检测页面何时使用PHP更新.我在Google上研究了一些东西,但没有发现任何东西.

我想要做的是在页面更新时调用特定的函数.我将运行一个cron作业来运行代码.

我想要这样的东西:

if (page updated) {
//functions
}
else
{
//functions
}
Run Code Online (Sandbox Code Playgroud)

如果我不能做那样的事情那么我想至少知道如何检测页面何时用PHP更新.请帮忙!

Sam*_*ane 5

使用file_get_contents()获取页面内容,从中创建MD5哈希,并将其与您已有的哈希进行比较.我建议将这个哈希存储在一个简单的文件中.

$contents = file_get_contents('http://site.com/page');
$hash     = file_get_contents('hash'); // the text file where the hash is stored
if ($hash == ($pageHash = md5($contents))) {
  // the content is the same
} else {
  // the page has been updated, do whatever you need to do
  // and store the new hash in the file
  $fp = fopen('hash', 'w');
  fwrite($fp, $pageHash);
  fclose($fp);
}
Run Code Online (Sandbox Code Playgroud)

不要忘记将allow_url_fopen设置为On.

  • 请注意,因为某些主机可能不允许这种远程文件访问,从而导致错误行为 (2认同)