Jump to content

Php detect browser close


wicimice

Recommended Posts

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

<?php

require('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');
}
?>

Link to comment
Share on other sites

“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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.