?
Solved

session exipiration

Posted on 2014-07-30
14
Medium Priority
?
265 Views
Last Modified: 2014-07-30
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
0
Comment
Question by:M. Jayme Nagy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40230076
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
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40230086
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
 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 40230087
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 58

Expert Comment

by:Gary
ID: 40230098
What is you are wanting to do when the sesson expires?
0
 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 40230101
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
 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 40230102
i want to set a mysql database value to logged-off

so when users login they can see all the active members
0
 
LVL 58

Expert Comment

by:Gary
ID: 40230114
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 1332 total points
ID: 40230118
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
 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 40230128
Should i use a cron job to reload the script or should i do it from within the script?
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40230140
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40230164
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40230171
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
 
LVL 58

Accepted Solution

by:
Gary earned 1332 total points
ID: 40230172
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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 668 total points
ID: 40230192
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

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question