如何检测页面何时使用 PHP 更新 [英] How to detect when a page gets updated with PHP
问题描述
我想知道如何检测页面何时使用 PHP 更新.我在谷歌上研究过东西,但什么也没找到.
I was wondering how to detect when a page gets updated with PHP. I've researched things on Google, but came across nothing.
我想做的是在页面更新时调用特定函数.我将运行一个 cron 作业来运行代码.
What I want to do is call a specific function when a page gets updated. I will be running a cron job in order to run the code.
我想要这样的东西:
if (page updated) {
//functions
}
else
{
//functions
}
如果我不能这样做,那么我至少想知道如何检测页面何时使用 PHP 更新.请帮忙!
If I can't do something like that then I want to at least know how to detect when a page gets updated with PHP. Please help!
推荐答案
使用 file_get_contents() 获取页面的内容,从中创建一个 MD5 散列,并将其与您已有的散列进行比较.我建议将此哈希存储在一个简单的文件中.
Use file_get_contents() to get the page's content, create a MD5 hash from it, and compare it with the hash you already have. I suggest storing this hash in a simple file.
$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);
}
不要忘记设置 allow_url_fopen 到 On.
Don't forget setting allow_url_fopen to On.
这篇关于如何检测页面何时使用 PHP 更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!