Hi
Here's the background:
- On one page I collect some data from another server and display it
- If I would do this every time I open my page - it would take too much time
- So I do it once a day (as I'm sure the retrieved info changes exactly at midnight) and store the value in local file.
- If it's stored locally, I check if it's still valid and display it from a local file.
Here's the code:
<?php
if (is_file('dane/slowo_cache.txt')&&date("mdy")==date("mdy",filemtime('dane/slowo_cache.txt')))
{//the file exists and modification was today
//read from local file
$dzis = file_get_contents('dane/slowo_cache.txt');
}
else
{
//the file doesn't exist or was modified yesterday or earlier.
//get current info from external server
flush();
$zawartosc = file_get_contents('http://www.katolik.pl/');
//get data i'm interested in from the whole html page
preg_match('/fragm=(.*)\n" target="_top">czytania na dzi/',$zawartosc,$wyniki);
//just some processing
$czytania = $wyniki[1];
$czyt_arr = explode(";",$czytania);
$dzis = trim($czyt_arr[count($czyt_arr)-1]);
//open (and create if not exists) cache file
$fid = fopen('dane/slowo_cache.txt','w');
//store retrieved data in cache
fwrite($fid,$dzis);
fclose($fid);
//modification time should be set now to today, so the next time info will be read from file
}
//display data independent of where it came from
echo $dzis;
?>
And now the problem: the time on my local server isn't synchronized with the server I'm getting the info from. So let's just say its 0:01 am on my server and 11:59 pm on external server. User acceses my page, the data is read from external server, as the date changed, reads the old info, and sets it for the next day in cache. After that info is read by my script from file, even though it has changed few minutes later on external server.
Any ideas how to improve this? Changing the system clock is not an option. The info might very rarely be the same from one day to other (so comparing new value to old value while writing to cache is out too).
I want to keep the script as simple as possible.
Thanks in advance.