1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
$data_source_file = 'welcomeMessage.php';
while (true) {
$last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;
// PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache
clearstatcache();
// get timestamp of when file has been changed the last time
$last_change_in_data_file = filemtime($data_source_file);
// if no timestamp delivered via ajax or data.txt has been changed SINCE last ajax timestamp
if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {
ob_start();
$data = ob_get_contents($data_source_file);
ob_end_flush();
$result = array(
'data_from_file' => $data,
'timestamp' => $last_change_in_data_file
);
$json = json_encode($result);
echo $json;
break;
} else {
sleep( 1 );
continue;
}
} |