Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default 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.
    Brett Porter Guest

  2. Similar Questions and Discussions

    1. #16263 [Com]: session.start() create new empty session file and not resume existing session
      ID: 16263 Comment by: pat at burnttech dot com Reported By: kur at natur dot cuni dot cz Status: No Feedback...
    2. Problem with global.asa database and Session OnEnd
      Agggghhhh.... I've read countless posting and still can't get an answer that works. I'm trying to update a record when a session ends. Here's the...
    3. Webmethod OnEnd
      Hello, I have a requirement on my webservice. I want to track when a request had come to my webmethod and when the response was completed. I...
    4. #25307 [NEW]: Crash when session.serialize_handler=wddx & session, post, get vars
      From: cristea at pntcd dot ro Operating system: any PHP version: 4CVS-2003-08-29 (stable) PHP Bug Type: Session related Bug...
  3. #2

    Default Re: Session OnEnd

    Brett Porter wrote:
    > 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.
    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_item) 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 - [email]spam@koivi.com[/email]
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Justin Koivisto Guest

  4. #3

    Default Re: Session OnEnd

    On 11 Nov 2003 12:19:22 -0800, [email]brett.porter@strikedesigns.co.uk[/email] (Brett Porter)
    wrote:
    >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.
    Probably not quite what you're after, but look at:

    [url]http://www.php.net/session-set-save-handler[/url]

    Would be nice if you could hook into that (in your case in the destroy
    handler), but still call the default session data handling.
    >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.
    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.Timeout vs.
    session.gc_maxlifetime).

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

  5. #4

    Default Re: Session OnEnd

    [email]brett.porter@strikedesigns.co.uk[/email] (Brett Porter) wrote in message news:<a4930814.0311111219.3d83ffbb@posting.google. com>...
    > 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.
    When user logins to the site, store his session_id in DB. (Look
    at Martin's login script
    [url]http://martin.f2o.org/download/php-login-script/[/url] ). And see if the
    stored session for the user is active or not. For that you may use my
    IsSessionActive() function found at
    [url]http://groups.google.com/groups?selm=abc4d8b8.0310130436.5096fa2b%40posting .google.com[/url]

    Also, look at this thread
    [url]http://groups.google.com/groups?threadm=3FAFE1F0.E8E8F165%40venus.ci.uw.edu .pl[/url]

    ---
    "Success = 10% sweat + 90% tears"
    Email: rrjanbiah-at-Y!com
    R. Rajesh Jeba Anbiah Guest

  6. #5

    Default 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_save_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_save_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
    Brett Porter Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139