Session OnEnd

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brett Porter

    Session OnEnd

    Howdy,

    For a challenge I took on a very large project and have decided to
    develop it on Red Hat, using PHP and MySQL. I am pleased to say that
    using Red Hat, PHP and MySQL has been extremely painless and a general
    pleasure to use.

    One thing has stumped me though.

    Part of my site uses a forum/chat-area which highlights which users
    are logged on and off of the site. Detecting when people logged off
    would have been easy using ASP as I would use the Session_OnEnd
    function within global.asa. But I am now horrified to discover that
    PHP does not support this in anyway.

    Surely this cannot be! If PHP lacks this fundamental ability I'm
    afraid I am going to have to act like an immature schoolboy and slap
    my forehead shouting 'Duh!!!' repeatedly.

    I have read a few places that suggest using a timer to automatically
    log people out of the database after ten minutes of inactivity. But I
    simply refuse to believe there isn't a more elegant solution.

    So any help or advice would be great.
  • Andy Hassall

    #2
    Re: Session OnEnd

    On 11 Nov 2003 12:19:22 -0800, brett.porter@st rikedesigns.co. uk (Brett Porter)
    wrote:
    [color=blue]
    >For a challenge I took on a very large project and have decided to
    >develop it on Red Hat, using PHP and MySQL. I am pleased to say that
    >using Red Hat, PHP and MySQL has been extremely painless and a general
    >pleasure to use.
    >
    >One thing has stumped me though.
    >
    >Part of my site uses a forum/chat-area which highlights which users
    >are logged on and off of the site. Detecting when people logged off
    >would have been easy using ASP as I would use the Session_OnEnd
    >function within global.asa. But I am now horrified to discover that
    >PHP does not support this in anyway.[/color]

    Probably not quite what you're after, but look at:



    Would be nice if you could hook into that (in your case in the destroy
    handler), but still call the default session data handling.
    [color=blue]
    >Surely this cannot be! If PHP lacks this fundamental ability I'm
    >afraid I am going to have to act like an immature schoolboy and slap
    >my forehead shouting 'Duh!!!' repeatedly.
    >
    >I have read a few places that suggest using a timer to automatically
    >log people out of the database after ten minutes of inactivity. But I
    >simply refuse to believe there isn't a more elegant solution.[/color]

    Other than:

    (a) explicit logoffs by the user
    (b) timeouts

    What other alternatives are there anyway? You can't detect when the user
    closes their browser, after all. I see your point about having a callback when
    the session is closed, but not about how it gets closed; ASP and PHP both have
    a timeout after which the session is deleted (Session.Timeou t vs.
    session.gc_maxl ifetime).

    --
    Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

    Comment

    • Justin Koivisto

      #3
      Re: Session OnEnd

      Brett Porter wrote:[color=blue]
      > Part of my site uses a forum/chat-area which highlights which users
      > are logged on and off of the site. Detecting when people logged off
      > would have been easy using ASP as I would use the Session_OnEnd
      > function within global.asa. But I am now horrified to discover that
      > PHP does not support this in anyway.
      >
      > I have read a few places that suggest using a timer to automatically
      > log people out of the database after ten minutes of inactivity. But I
      > simply refuse to believe there isn't a more elegant solution.[/color]

      This is how I handle it...

      I use customized session handlers that store all the session information
      into a database table:
      CREATE TABLE psa_sessions (
      sesskey varchar(32) NOT NULL default '',
      user_name varchar(16) NOT NULL default '',
      expiry int(11) NOT NULL default '0',
      value text NOT NULL,
      UNIQUE KEY sesskey (sesskey)
      )

      In the session read and write functions, I update the expiry of the
      session record. te gc function (of course) deletes the record.

      I then turn up the gc_probability (usually to 100% or until I notice a
      difference). I have the session timeout set to what I *think* the
      maximum time ($session_exp_i tem) someone would be on a single page
      (depends on the type of site, etc.).

      Then to see who's online, it's just a matter of checking the user_name
      field in the session table. Since the timeouts are set reasonable, and
      gc is running all the time, it's fairly accurate.

      --
      Justin Koivisto - spam@koivi.com
      PHP POSTERS: Please use comp.lang.php for PHP related questions,
      alt.php* groups are not recommended.

      Comment

      • R. Rajesh Jeba Anbiah

        #4
        Re: Session OnEnd

        brett.porter@st rikedesigns.co. uk (Brett Porter) wrote in message news:<a4930814. 0311111219.3d83 ffbb@posting.go ogle.com>...[color=blue]
        > Howdy,
        >
        > For a challenge I took on a very large project and have decided to
        > develop it on Red Hat, using PHP and MySQL. I am pleased to say that
        > using Red Hat, PHP and MySQL has been extremely painless and a general
        > pleasure to use.
        >
        > One thing has stumped me though.
        >
        > Part of my site uses a forum/chat-area which highlights which users
        > are logged on and off of the site. Detecting when people logged off
        > would have been easy using ASP as I would use the Session_OnEnd
        > function within global.asa. But I am now horrified to discover that
        > PHP does not support this in anyway.[/color]

        When user logins to the site, store his session_id in DB. (Look
        at Martin's login script
        http://martin.f2o.org/download/php-login-script/ ). And see if the
        stored session for the user is active or not. For that you may use my
        IsSessionActive () function found at


        Also, look at this thread


        ---
        "Success = 10% sweat + 90% tears"
        Email: rrjanbiah-at-Y!com

        Comment

        • Brett Porter

          #5
          Re: Session OnEnd

          Hi,

          Thanks, for your suggestions, they certainly have given me food for
          thought.

          "Explicit logoffs by the user" I don't see as an option - you know
          what users are like!

          "session_set_sa ve_handler" looks like the solution I am after, I am
          certainly warming to the idea of using my MySQL database to store
          sessions rather than files as it ties in very well with my project
          especially as the data stored within the session variables come from
          the database!.

          If I was to continue using files to save sessions is there any way I
          can use the session_set_sav e_handler for the destroy function only so
          that I don't have to rewrite the other functions?

          I like the IsSessionActive () function, very clever, one concern I do
          have though is that the site will be receiving a projected 2000+
          visitors each day which I think *might* start to use valuable server
          resources.

          Again, many thanks,

          Brett

          Comment

          Working...