Solved

session exipiration

Posted on 2014-07-30
14
264 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 83

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
Industry Leaders: 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 333 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 83

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 110

Expert Comment

by:Ray Paseur
ID: 40230164
0
 
LVL 110

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 333 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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 167 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 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