Basically I want to log someone out of my site if my server doesn't receive a "ping" every 5 minutes from a javascript.
-
It is your PHP that does the redirect (by sending a location header), or is it when the user clicks on a link in the page that you want to trap? Server-side (trapping headers sent by your code) is in PHP, client-side (trapping user clicks) is javascript. You added both tags - but which one is it? – Robbie Sep 27 '12 at 03:03
-
Can't you store the last access time in your SQL table, and check if the difference between then and now is more or less than 5 minutes? – meiamsome Sep 29 '12 at 00:08
2 Answers
If a user types an address into the address bar, you have no control over this without some shady tactics that no one is going to help you write.
One possible solution is to have a simple setTimeout(func(),60000); where the func() is an asynchronous call to the server. That way, as long as the user is still on the page, they are "invisibly" pinging your site once every minute or something. Then you know, if the user hasn't pinged in over 2 minutes or whatever, that they have left your site.

- 12,255
- 7
- 53
- 65
-
Basically I have an sql table that I want to be edited on logout... so how could I implement this "pinging" method to log them off if the leave the page without loging off? – Timberwolf Sep 27 '12 at 19:29
A location header is part of the HTTP response to a request.
What you want can mostly be done in JavaScript, but isn't recommended.
window.onbeforeunload = function() {
// do something here
}
I don't believe you can know where the user is headed after your page. That would be a potential security issue. See also: https://stackoverflow.com/a/277201/362536