By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,116 Members | 1,319 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,116 IT Pros & Developers. It's quick & easy.

Session OnEnd

P: n/a
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.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 11 Nov 2003 12:19:22 -0800, br**********@strikedesigns.co.uk (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:

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

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 (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #2

P: n/a
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 - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #3

P: n/a
br**********@strikedesigns.co.uk (Brett Porter) wrote in message news:<a4**************************@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
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
http://groups.google.com/groups?selm...ing.google.com

Also, look at this thread
http://groups.google.com/groups?thre...s.ci.uw.edu.pl

---
"Success = 10% sweat + 90% tears"
Email: rrjanbiah-at-Y!com
Jul 17 '05 #4

P: n/a
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
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.