On Thu, Feb 11, 2010 at 06:37:39AM +0200, Teus Benschop wrote:

> Good day,
> May I ask you, gurus, whether it is possible to set a flag in PHP that
> persists for the duration of the server being switched on?
> If the server would be power cycled, the flag would be off. Similarly if
> the web server would be restarted the flag would be off also.
> I do realize that such a thing would be possible to be done in MySQL by
> using a table with flags in memory: CREATE TABLE IF NOT EXISTS timer
> (flag int) ENGINE = MEMORY;
> But is this also possible in PHP?
> Thanks for any help,

The only way I can imagine to do this is to dump some values into a file
in the web root of your site. This would be something like the interval
since the server/webserver was started. You'd issue shell commands to
find this value, and then compare it to what's in the file. If the
file contains a larger interval than what you've just found by running
the shell commands, you know the server/webserver's been restarted, and
how long since that's happened. That is, if the file contains a longer
interval, it's "stale". You'd then replace its contents. Otherwise,
you'd leave it alone.

To actually *get* the interval(s) you're looking for would be quite a
trick. On a Linux/BSD server, you'd have to use a combination of shell
commands, possibly including uptime, ps and top. The uptime command will
give you the up time for the computer, but is only good at a resolution
of about a minute. I'm not sure how to get reliable up times out of
'ps'; the command has about a thousand options. For computer uptime,
search for the 'init' process (usually process ID 1). For the webserver,
search for apache, apache2, or whatever webserver is running.
Unfortunately, this is complicated by the fact that apache typically
runs several processes, and process IDs aren't a reliable way to
determine which one started first, since process IDs recycle.

If you have cron available to you, you can do a crontab entry which
starts with the string '@reboot'. This could dump a date/time into your
file. But it won't help with apache.

Yes, I know you asked for a yes/no flag. If you must have this, then
have your cron command simply dump 'no' into your file. As for apache, I
suspect you're stuck with calculating intervals to determine the flag,
as described above.

Maybe someone else has a better idea.


Paul M. Foster

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to