Reputation: 1401
Ok, I read this post and someone suggest using this code to check if user left page:
if($_SERVER['REQUEST_URI'] !== 'page_with_session.php'){
session_destroy(); // Kill session for all pages but page_with_session.php
}
Now can someone please explain how REQUEST_URI works cause I can't seem to find it in the PHP manual, or can someone suggest another way to check when a user has left a page.
Please note I can't use Javascript for this project.
Upvotes: 0
Views: 1513
Reputation: 32552
The only real way to do this is to keep a really short session timeout, and then have either an embedded iframe with a meta refresh, or a javascript call out to your PHP page to keep it alive.
Upvotes: 1
Reputation: 12525
$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI/1.1 specification, so you should be able to expect those.
$HTTP_SERVER_VARS contains the same initial information, but is not a superglobal. (Note that $HTTP_SERVER_VARS and $_SERVER are different variables and that PHP handles them as such)
'REQUEST_URI' The URI which was given in order to access this page; for instance, '/index.html'.
(c) php.net
As you see it's perfectly explained. When user leaves page you are not able to do what you want. So as @jimpic's told you use sessions.
Upvotes: 1
Reputation: 5520
With that code you can only tell when a user changes to another page on your server. If he leaves to another website, or closes the tab/browser, this won't work. Use javascript instead or use a session timeout. REQUEST_URI is the URI of the current request, so if a user changes to another page on your server, you can check that it's not the "session page" and destroy the session. this will only work if you don't use rewrite or similar techniques.
Upvotes: 3