如果特定文件的内容已更改,PHP将自动刷新页面

时间:2017-03-21 13:08:05

标签: php

我正在尝试确定特定文件的内容是否已更改,而不是自动刷新显示消息的php页面。

我想把它作为一个单独的php页面加载后,可以检查“test.txt”是否被更改。

如果有人在“test.txt”中添加或删除了数据,我需要自动刷新显示“message.php”的div。

基本上我需要让“index.php”检查“test.txt”是否被更改。如果是,我必须自动刷新显示“message.php”的div或至少刷新整个“index.php” 这是我正在使用的:

- 一个名为“test.txt”的文本文件,其中添加或删除了文本

-a“check_md5.php”检查文件是否已更改:

<?php
function file_was_changed($file_to_check,$interval_check_md5)
{
    //intializez md5 variables to  0
    $valoare1_md5=$valoare2_md5=0;

    //detect first md5 value
    $valoare1_md5=(md5_file($file_to_check));
    echo '<p>FILE "'.$file_to_check.'" => MD5 1 ='.$valoare1_md5.'</p>'.PHP_EOL;

    //wait for the given interval
    sleep($interval_check_md5);

    //detect second MD5 value
    $valoare2_md5=(md5_file($file_to_check));
    echo '<p>FILE "'.$file_to_check.'" => MD5 2 ='.$valoare2_md5.'</p>'.PHP_EOL;

    //compare MD5 values and determine and set boolean accordingly
    if ($valoare1_md5==$valoare2_md5) 
    {
        return false;
        echo 'FILE WAS NOT CHANGED';
    } 
    else 
    {
        return true;
        echo 'FILE WAS CHANGED';
    };
}

?>

<?php

$file_changed=(file_was_changed('test.txt',1));
if ($file_changed==true)
{
    echo 'FILE WAS MODIFIED';
} 
else 
{
    echo 'FILE IS UNCHANGED';
};

?>  

-a“message.php”显示消息“HELLO”:

<?php
    echo 'HELLO';
?>

-a“index.php”包含2个div,其中加载了“check_md5.php”和“message.php”:

<html>


<style>
.md5_status, .display {
  position: absolute;
  left: 0;
  right: 0;
}

.md5_status {
  top: 0;
  height: 15%;
  background-color: pink;  
 /* overflow:scroll; */
}

.display {
  bottom: 0;
  height: 85%;
  background-color: #ddd;    
}
</style>

<body>
    <div id="links" class="md5_status">
    <?php //include("verificare_md5.php"); ?>
    <?php include("check_md5.php"); ?>
    </div>

    <div id="contents" class="display">  
    <?php //include("extrage_date_din_xml.php"); ?>
    <?php include("message.php"); ?>
    </div>
</body>
<html> 

我该怎么做?感谢您抽出宝贵时间阅读我的整个问题。

2 个答案:

答案 0 :(得分:1)

You'll need cooperation from the server-side script, but it is possible to eliminate most of the transfers using conditional requests. HTTP has a feature which allows the server to only send a response if it hasn't changed.

For the PHP part, you can use e.g. this module (or reimplement the conditional logic yourself).

For the AJAX part, if you use GET (as POST is non-cacheable), the response will be cached and data will only be transferred if it has changed, else it will come from the local browser cache.

There are some caveats, especially if your pages are served with an Expires header

答案 1 :(得分:1)

我的问题的解决方案可以在这里找到:Refresh page when directory content changes 我已经调整了代码,它完美无缺! 谢谢Noata!

我使用索引页面显示2个div。第一个div包含有关从&#34; check_file_stat.php&#34;收集的文件时间戳的信息。使用stat php&#34; stat&#34; function。第二个div加载来自&#34; message.php&#34;的数据。

- &#34; index.php&#34;的内容:

<script language="javascript">

//var myVar=setInterval(function(){chekUpdate()},5*60*1000); // at 5 minutes intervals
var myVar=setInterval(function(){chekUpdate()},1000);
var stat_old = "";
function chekUpdate()
{
    $("#status_fisier").load("check_file_stat.php",function(){
        var stat_new = $("#status_fisier").html();
        if((stat_old != "") && (stat_old != stat_new)){
            refreshSlideShow();
        }
        stat_old = stat_new;
    });
}
function refreshSlideShow()
{
    // you can refresh your slideshow here.

    location.reload(); 

}
</script>


<body>



<style>
#status_fisier, #display_ticker {
  position: absolute;
  left: 0;
  right: 0;
}

#status_fisier {
  top: 0;
  height: 10%;
  background-color: pink;  
 /* overflow:scroll; */
}

#display_ticker {
  bottom: 0;
  height: 90%;
  background-color: #ddd;    
}
</style>

<div id="status_fisier">

</div>
<div id="display_ticker">
<?php include("message.php"); ?>
</div>

</body>

- &#34; check_file_stat.php&#34;的内容:

<?php
$locatie_fisier_de_verificat='test_fisier.txt';
/*

The stat() function returns information about a file.

This function returns an array with the following elements:

    [0] or [dev] - Device number
    [1] or [ino] - Inode number
    [2] or [mode] - Inode protection mode
    [3] or [nlink] - Number of links
    [4] or [uid] - User ID of owner
    [5] or [gid] - Group ID of owner
    [6] or [rdev] - Inode device type
    [7] or [size] - Size in bytes
    [8] or [atime] - Last access (as Unix timestamp)
    [9] or [mtime] - Last modified (as Unix timestamp)
    [10] or [ctime] - Last inode change (as Unix timestamp)
    [11] or [blksize] - Blocksize of filesystem IO (if supported)
    [12] or [blocks] - Number of blocks allocated


*/
$stat = stat($locatie_fisier_de_verificat);
echo '<p>Verificare stare fisier: "'.$locatie_fisier_de_verificat.'"</p>';
echo '<p>Timpul ultimei modificari (UNIX Timestamp): ' . $stat['mtime'] . "</p>"; /* time of last modification (Unix timestamp) */
echo '<p>Dimensiune in bytes: ' . $stat['size'] . "</p>";  /* size in bytes */
?>

- &#34; message.php&#34;的内容:

<?php
    echo 'HELLO';
?>

再次,非常感谢你和Noata在这里发布的解决方案:Refresh page when directory content changes