Re: Application Scope variables ?

From: Gordon Burditt (gordonb.a6hin_at_burditt.org)
Date: 09/21/04


Date: 21 Sep 2004 18:55:00 GMT


>Just to stream the discussion up - how a page hit conter in implemented in PHP?

I'd use a database:
        UPDATE hit_count set count=count+1, last=now() where url='...';
or if I really wanted details for each hit, add an entry to a hit_log table,
logging time, URL, and perhaps other stuff like IP, user name, session, referrer, etc.

>In ASP you increment a relative Application scope variable every time a page is
>requested. This veraible is accessible from any session.

Besides hit counters, of what *USE* is such a variable?
It can be accessed from (and messed up by) so many different
concurrent sessions that any significant read/write use will have
to worry about locking issues. Does ASP even guarantee that several
sessions each doing the equivalent of $hitcount++; will be atomic
and not lose counts (on a multiprocessor machine, possibly)?

Assume $last_server is the ID of the last waitperson assigned to a table (and
it's one of those "Application-scope" variables.
IDs run from 1 to $max_server. You want to assign them in turn, round-robin.

        $last_server++;
        if ($last_server > $max_server) { $last_server = 1; }
        $this_server = $last_server;
... assign $this_server to serve the food for this order ...

Now, how do you write that (or its equivalent in ASP) so it's
multi-session, multi-processor safe? You don't assign the same
server two orders a row. You don't skip anyone. Not even if a bunch
of orders come in simultaneously.

I also wouldn't be too pleased to learn that I'd lose several days
worth of changes to that variable (kept only in memory) if the
machine crashed. In particular, losing a variable like 'next invoice
number to assign' kept in memory only would be a real nuisance.

>This variable is sitting in the memory as long as the Application (i.e. the website) is
>running.

Which means you can lose it at any time if the Application is running.

>If the Application is stopped, it fires an event "application_on_close" and on this
>event you write an application data to a file from which it can be recovered when the
>application is restarted.

And if the application, or system, crashes? How often do you actually intentionally
shut down a web server (besides applying security patches)? In my experience, this
is MUCH less often than CPU fan failure or power supply failure.

>How do I get the same effect in PHP?

What do you need it for? I think it makes an unreliable hit counter and it's not
good for much else.

                                                Gordon L. Burditt



Relevant Pages

  • Re: Application Scope variables ?
    ... >Just to stream the discussion up - how a page hit conter in implemented in PHP? ... This veraible is accessible from any session. ... number to assign' kept in memory only would be a real nuisance. ... is MUCH less often than CPU fan failure or power supply failure. ...
    (comp.lang.php)
  • Re: Business Objects and Session Variables
    ... The DB hit is not normally too bad on most modern networks. ... You can use Session to easily cache without building anything. ... ViewState (for the scope of a single page). ... > Storing everything in session may give you huge performance hits, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Incomlete object?
    ... have you tried commenting code and uncommenting until you hit the error. ... | I have a few classes, and I keep objects of those in session. ... | index.php -> class Page, starts session, in constructor inits database ... | point I get an error that it is incomplete (DB is stored in session as ...
    (alt.php)
  • Re: Are Test matches getting too predictable?
    ... in the morning session on day-1, or "score as many as possible in 20 ... try to hit out for 20 overs, run the singles for the next 20, and then ... hit out again in the last 10. ... result but, in my view, a bigger gain in more competent cricket. ...
    (rec.sport.cricket)
  • Re: this is what u get from pap mp
    ... A RAG-AND-BONE man accused of criminal intimidation said yesterday that he was made to wait almost four hours to see his Member of Parliament at a Meet-the-People session. ... When he got to see the MP, Jalan Besar GRC's Denise Phua, he ranted at her that the $200 he got from a government agency in financial aid was not enough, which spurred one of her grassroots helpers to shout at him just as he was leaving. ... 'How can you not hit her? ... He then decided to call the Prime Minister's Office to tell the Prime Minister - the 'boss of all MPs' - why people were treating their MP like that. ...
    (soc.culture.singapore)