session exipiration

Hi,

Is it possible to get your server to update an mysql database on a session timeout without any user input?

ie.
 1)The server detects all active sessions
 2)An active session is ignored
 3)Once a session is destroyed/inactive then the server initiates the mysql update function

thanks in advance
LVL 6
J NUnicorn wranglerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
Not really, you could have some js at the client that counts down from 24 minutes and then sends an ajax request to the server
But if the user just closes the tab/browser it won't work (reliably)
Or use your own session handler i.e. store the data (last accessed time etc) in a db and have a script run on a regular basis through the cron to do whatever it is you need to do when the 'session' time has expired
Or maybe this way
http://php.net/manual/en/function.session-set-save-handler.php
0
Dave BaldwinFixer of ProblemsCommented:
PHP sessions?  No.  PHP sessions are expired on the server by deleting the session info that is stored in files.  But that is done on a somewhat random basis to avoid spending too much time on it.  And it is basically a background process.  The only control over it is in 'php.ini' where you can set three items starting with 'session.gc_', 'gc' is for garbage collection'.  http://php.net/manual/en/ini.list.php  Although even that can be a little misleading.  If you are on shared hosting, 'session.gc_maxlifetime' is actually controlled by the shortest/lowest value.  If you try to set a high value, it will be ignored.
0
J NUnicorn wranglerAuthor Commented:
Hi,

I was thinking of setting a cron job to do this.. if i was to do a cron job how do you think i should set it up
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

GaryCommented:
What is you are wanting to do when the sesson expires?
0
J NUnicorn wranglerAuthor Commented:
FYI:
Currently i store a session variable that is reset on each pageload. if the variable and the current time vary more than 24 hours and the person reloads the page they will be 'logged off' and the session is destroyed. however, if the user closes the browers or never reloads the page then this never happens
0
J NUnicorn wranglerAuthor Commented:
i want to set a mysql database value to logged-off

so when users login they can see all the active members
0
GaryCommented:
Well you shouldn't be using sessions for that anyway. Sessions should be temporary objects.  Anything more should be cookie based.

Simplified workflow...
Store the user id and access time in the db, on each page load reset the access time.
When the user comes to page check the last access time is not more than 24 hours or non existant, if thats the case then force them to login again.
Here you could set a session to save checking on each page load while they are active

Create a new php page to cycle through any rows in the db table where the access time is +24 hours ago and remove from the table

I'm saying 24 hours but change that to be whatever you consider non active.
0
GaryCommented:
In that case...

Amend your users table to store the last access time and use that to show who is active or not within the last 20 minutes

No need to mess around with anything else
0
J NUnicorn wranglerAuthor Commented:
Should i use a cron job to reload the script or should i do it from within the script?
0
Dave BaldwinFixer of ProblemsCommented:
I agree with Gary.  In addition, a CRON job can't properly delete sessions.  That is done in the PHP interpreter and I don't know of any way you can tell it to do that.
0
Ray PaseurCommented:
How HTTP Client/Server Protocol works:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html

When you consider these two things together, you'll fairly quickly come to the understanding that you can't really "do something" when "nothing" is happening.  It's possible to do something when something happens, and that something can include starting up garbage collection.  Most likely you will find a solution in a design pattern that is similar to the PHP session garbage collector.
0
GaryCommented:
No need for anything but storing and updating the last access time,
When you want to show who is active then do a select from the users table whose last access time was within the last 20 minutes or whatever.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
No need for anything but storing and updating the last access time,
Unless you want an historical record, then you want a log table.  And if you have a log, you will want to prune it periodically.  In any case this is not the sort of thing that the PHP session handler is used for, and the database advice is the better approach.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.