Welcome to the Cumulus Support forum.

Latest Cumulus MX V4 release 4.4.2 (build 4085) - 12 March 2025

Latest Cumulus MX V3 release 3.28.6 (build 3283) - 21 March 2024

Legacy Cumulus 1 release 1.9.4 (build 1099) - 28 November 2014
(a patch is available for 1.9.4 build 1099 that extends the date range of drop-down menus to 2030)

Download the Software (Cumulus MX / Cumulus 1 and other related items) from the Wiki

If you are posting a new Topic about an error or if you need help PLEASE read this first viewtopic.php?p=164080#p164080

PHP Experts...

Other discussion about creating web sites for Cumulus that doesn't have a specific subforum

Moderator: daj

Post Reply
User avatar
mcrossley
Posts: 14388
Joined: Thu 07 Jan 2010 9:44 pm
Weather Station: Davis VP2/WLL
Operating System: Bullseye Lite rPi
Location: Wilmslow, Cheshire, UK
Contact:

PHP Experts...

Post by mcrossley »

I want PHP script that once invoked by a client..

1. Runs in a loop, with a delay - I can do this bit!
2. Each time round the loop checks if a file on the web server has been updated - how?
3. If updated, reads the file - OK, normal fopen() stuff.
4. Dos some 'stuff' with the contents - OK.

So step 2, I assume I would use filectime(), but to save me the work (I'm no PHP coder) does anyone have a bit of precanned code to do this?
gemini06720
Posts: 1700
Joined: Mon 10 Aug 2009 10:16 pm
Weather Station: No weather station
Operating System: No operating system
Location: World...

Re: PHP Experts...

Post by gemini06720 »

Mark, I am certainly no expert either... :mrgreen:

You can certainly use the change time of file (filectime) and possibly the file modification time (filemtime).

But, for both of those functions properly, you will need to keep a pointer (such as a small file) where the last time check will be written to - it would then be a matter of reading the pointer file (with the previously recorded update time information) and comparing its information with either the 'filectime' (or 'filemtime') of the file to be checked/watched (updated).

In one of my scripts, I check the number of minutes that have elapsed between the time a METAR file has been downloaded and save into a cache directory and the present time the script is running - if that time exceed a pre-determine number of minutes, then the script download (and save) a new copy of the METAR file. I understand that is not exactly what you need, but that might 'generate' some ideas/suggestions... ;)

Here is an excerpt the code used in that script:

Code: Select all

  $metarRefresh = 20; // time between refresh, in minutes
  $download = true; // set pointer to download a fresh copy of the file
  if (file_exists($metarFile)) { // check that the metar file already exist
    $metarTime  = filemtime($metarFile); // get the last modification time of the metar file
    $reloadTime = $metarTime + ($metarRefresh*60); // refresh time set to 1200 seconds - check time difference
    $now = mktime(); // get the time now
    if ($now < $reloadTime) { // has the time been exceeded
      $metar = getTxtFile($metarFile); // if not, get/read the saved file from the cache
      $download = false; // set pointer to not download a fresh copy of the file
    }
  }
And further down, I have something like:

Code: Select all

  if ($download) { // download a fresh copy of the METAR file if that file is stale or missing
.....
  }
User avatar
mcrossley
Posts: 14388
Joined: Thu 07 Jan 2010 9:44 pm
Weather Station: Davis VP2/WLL
Operating System: Bullseye Lite rPi
Location: Wilmslow, Cheshire, UK
Contact:

Re: PHP Experts...

Post by mcrossley »

Thanks Ray, I was thinking my script will only need to track the file change time for the duration of the of the script execution - which I plan to limit to 1 minute, at which point the client would automatically re-invoke the script if required. But thinking about this it may be more complicated than I first thought, as each invocation of the script may or may not be by the same client.

That is the script would have to 'know' if this was the first time it was being called for a client, or if it was being re-invoked after a time out.

I may resort to performing the update check at the client side, that removes the need for session tracking on the server.
gemini06720
Posts: 1700
Joined: Mon 10 Aug 2009 10:16 pm
Weather Station: No weather station
Operating System: No operating system
Location: World...

Re: PHP Experts...

Post by gemini06720 »

Or you could check from where (whom) the request for the script is coming from...

Client side might be a better way of doing this ... although I really dislike 'imposing' on the client unless absolutely needed (no other way to do it)...
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: PHP Experts...

Post by beteljuice »

The beteljuice would use filemtime, and the methodology of the 'cachefile' script.

You don't say if it is necessary for the file to be processed every minute, or 'latest' on-demand.
If the former, the cachfile code would be unsuitable.
Image
......................Imagine, what you will KNOW tomorrow !
User avatar
mcrossley
Posts: 14388
Joined: Thu 07 Jan 2010 9:44 pm
Weather Station: Davis VP2/WLL
Operating System: Bullseye Lite rPi
Location: Wilmslow, Cheshire, UK
Contact:

Re: PHP Experts...

Post by mcrossley »

my idea was to process the file as soon as it was updated, so put a sleep in the detection loop of about 1 sec, then push it out to the client. Thinking realtime.txt here.
serowe
Posts: 484
Joined: Tue 03 Aug 2010 6:23 am
Weather Station: WM918
Operating System: Win Server 2008 R2
Location: Ferntree Gully, VIC, Oz
Contact:

Re: PHP Experts...

Post by serowe »

Only just read this - you need to be VERY careful your script doesn;t run away with itself and hog the servers CPU. Been there, done that - and then you find out how good (or bad!) your ISP really is with errant users :)
Punctuation is the difference between 'Let's eat, grandma' and 'Let's eat grandma'
User avatar
mcrossley
Posts: 14388
Joined: Thu 07 Jan 2010 9:44 pm
Weather Station: Davis VP2/WLL
Operating System: Bullseye Lite rPi
Location: Wilmslow, Cheshire, UK
Contact:

Re: PHP Experts...

Post by mcrossley »

I decided that it was too complex, and not enough browsers implement push technology yet, so reverted to good old Ajax
Post Reply