wicimice Posted December 29, 2014 Share Posted December 29, 2014 I have a problem, where i need to know when user leave or close tha browser tab. I tryd to make a ajax post, after unload, make a POST into kill_browser.php and after 1 minute when session_expire, php code will be execute and save time into database. But it not seems to work :/ I'm glad if some 1 can say what im doing wrong, or is there eny better way to detect it. @home.php var leaved = 'leaved';$(window).unload( function(){ $.ajax({ url: 'kill_browser.php', global: false, type: 'POST', data: leaved, async: false, success: function() { console.log('leaved'); } }); }); @kill_browser.php <?phprequire('class/connection.php');$conn = new connection();session_cache_limiter('private');$cache_limiter = session_cache_limiter();session_cache_expire(1);$cache_expire = session_cache_expire();session_start();$_SESSION['leaved'] = $_POST['leaved'];if($_SESSION['leaved'] == null || $_SESSION['leaved'] == " "){ $conn->logOut($_SESSION['userID'], $_SESSION['userIP'], $_SESSION['LAST_GENERATED_ID']); session_regenerate_id(true); unset($_SESSION); session_destroy(); header('location:index.php');}?> Quote Link to comment https://forums.phpfreaks.com/topic/293496-php-detect-browser-close/ Share on other sites More sharing options...
Jacques1 Posted December 29, 2014 Share Posted December 29, 2014 “Doesn't work” doesn't work as an error description. If you want help with a problem, you need to actually tell us what that problem is. Either way, the general approach already doesn't make a lot of sense. Why on earth would you terminate the session just because the user has closed some tab or window with your site in it? People today use lots of tabs at the same time, and they constantly open and close them. Just because I close, say, one of five phpfreaks tabs doesn't mean that I want to leave the site. In fact, I'd be seriously pissed off if I was automatically logged out while trying to submit a long reply. The implementation is also ... weird. You use the cache headers in an attempt to delay PHP execution? You generate a new session ID only to throw it away two lines later? You unset the internal $_SESSION superglobal? Are you sure you know what you're doing? What exactly is the goal here? I mean, why and for whom do you need this? Quote Link to comment https://forums.phpfreaks.com/topic/293496-php-detect-browser-close/#findComment-1501049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.