如果特定文件的内容发生更改,PHP会自动刷新页面

如果特定文件的内容发生更改,PHP会自动刷新页面,php,Php,我试图确定特定文件的内容是否已更改,然后自动刷新显示消息的php页面 我想让它成为一个php页面,在加载后,可以检查“test.txt”是否被更改 如果有人从“test.txt”中添加或删除数据,我需要自动刷新显示“message.php”的div 基本上,我需要让“index.php”检查“test.txt”是否被更改。如果是,我必须自动刷新显示“message.php”的div,或者至少刷新整个“index.php” 以下是我正在使用的: -一个名为“test.txt”的文本文件,其中添加或

我试图确定特定文件的内容是否已更改,然后自动刷新显示消息的php页面

我想让它成为一个php页面,在加载后,可以检查“test.txt”是否被更改

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

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

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

-检查文件是否更改的“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';
};

?>  

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


-包含两个div的“index.php”,其中加载了“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> 

.md5_状态,.display{
位置:绝对位置;
左:0;
右:0;
}
.md5_状态{
排名:0;
身高:15%;
背景颜色:粉红色;
/*溢出:滚动*/
}
.展示{
底部:0;
身高:85%;
背景色:#ddd;
}

我怎么做这个?谢谢你花时间阅读我的全部问题

我的问题的解决方案可以在这里找到:
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
我已经修改了代码,它工作得非常好! 谢谢你,诺塔

我使用索引页来显示2个div。第一个div保存有关使用stat php“stat”函数从“check_file_stat.php”收集的文件时间戳的信息。第二个div从“message.php”加载数据

-“index.php”的内容:


//var myVar=setInterval(函数(){chekUpdate()},5*60*1000);//每隔5分钟
var myVar=setInterval(函数(){chekUpdate()},1000);
var stat_old=“”;
函数chekUpdate()
{
$(“#status_fisier”).load(“check_file_stat.php”,function()){
var stat_new=$(“#status_fisier”).html();
if((stat_old!=“”)和&(stat_old!=stat_new)){
刷新幻灯片();
}
stat_old=stat_new;
});
}
函数refreshSlideShow()
{
//您可以在此处刷新幻灯片。
location.reload();
}
#状态_fisier,#显示_ticker{
位置:绝对位置;
左:0;
右:0;
}
#地位{
排名:0;
身高:10%;
背景颜色:粉红色;
/*溢出:滚动*/
}
#显示计时器{
底部:0;
身高:90%;
背景色:#ddd;
}
-“check_file_stat.php”的内容:


-“message.php”的内容:



再次感谢Noata在这里发布的解决方案:

您需要使用AJAX。请小心,在您的“文件”was\u changed函数中,您尝试在函数返回后回显某些内容。这些消息永远不会出现。@Nenroz:谢谢你的观察。我会改变这个。
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
<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>
<?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 */
?>
<?php
    echo 'HELLO';
?>