0

我想知道如何检测页面何时使用 PHP 更新。我在谷歌上研究过东西,但一无所获。

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

我想要这样的东西:

if (page updated) {
//functions
}
else
{
//functions
}

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

4

3 回答 3

6

使用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);
}

不要忘记将allow_url_fopen设置为On

于 2012-05-19T20:09:57.833 回答
2

正如您所说,您可以每小时或每 15 分钟或其他任何时间运行一次 cron。它必须访问该页面,获取其修改日期,将其与存储的值进行比较,如果不同,则执行某些操作。显然,您需要更新您的信息,并将页面的当前日期设置为 $last_set_date 或其他。(应该在数据库中完成)

这个快速片段获取页面的最后更新时间(这里有关于该主题的更多信息:get_headers 的 PHP 文档):

<?php
$url = 'http://www.example.com';
$h = get_headers($url, TRUE);
echo "LAST MODIFIED: ", $h["Last-Modified"];
?>

在你得到这个之后,你需要做的就是将它与之前存储的值(文本文件、数据库)进行比较,看看它是否不同。这就是page updated你的脚本中的检查!

随时通过评论给我更多的澄清,我会努力改进我的答案。

于 2012-05-19T20:23:50.170 回答
0

您不能简单地检测页面是否已使用 php 更新,请尝试使用 javascript

在这里,我假设您包含 jquery libray:

<script type="text/javascript">
     $( document ).ready( function(){
        $("#someElementInYourPageYouWannaWatch").change( function(){
          $.ajax({
            url:'http://yourwebsite.php/your_controller.php',
            ...
            success: function( response ){
              $("#someNotifyElement).html('updated');
            }
          });

        });
     });
</script>

类似的东西:)

于 2012-05-19T20:08:27.400 回答