Solved

session exipiration

Posted on 2014-07-30
14
256 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
  • 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 82

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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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 82

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 108

Expert Comment

by:Ray Paseur
ID: 40230164
0
 
LVL 108

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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now