manix Posted August 9, 2011 Share Posted August 9, 2011 I need to execute a logout.php file upon window closing and here's what I have <script type="text/javascript"> $(window).unload( function () { $.post('logout.php', { id: $('#loutid').html() }, function(returned) { }); }); </script> logout.php <?php if(isset($_POST['id'])) { mysql_query("UPDATE `z` SET `online`='0' WHERE `id`='$userid'"); } ?> I'm guessing that the problem is there's not enough time to execute the script when the user closes the window.. Bad thing is I can't think of anything I can do Quote Link to comment https://forums.phpfreaks.com/topic/244338-execute-script-when-window-is-closing/ Share on other sites More sharing options...
The Little Guy Posted August 9, 2011 Share Posted August 9, 2011 <script type="text/javascript"> $(window).unload( function () { $.ajax({ type: "POST", data: "id=<?php echo $_SESSION['id']; ?>", url: "logout.php" }); }); </script> Quote Link to comment https://forums.phpfreaks.com/topic/244338-execute-script-when-window-is-closing/#findComment-1255004 Share on other sites More sharing options...
JasonLewis Posted August 9, 2011 Share Posted August 9, 2011 In my opinion, a better way that doesn't rely on Ajax is to create another column in your table called 'visit_timestamp' or something similar. On each page request for a user that's logged in you'd update their last visit timestamp to the current time. That way you can run a query to update any users and set their online='0' when their visit_timestamp is say, 10 minutes old. This won't log them out, but it will keep your counter or whatever this is for reasonably accurate. Quote Link to comment https://forums.phpfreaks.com/topic/244338-execute-script-when-window-is-closing/#findComment-1255008 Share on other sites More sharing options...
Jumpy09 Posted August 9, 2011 Share Posted August 9, 2011 Jaysonic you were almost right, but that would require running the Query pretty often if you use ( 0 -> Offline , 1 -> Online )! What I do to keep track of online users is with a timestamp field labeled " LastActive ", when you run it through php given $row [ "LastActive" ] is the result you got back! <?php if ( strtotime ( $row [ "LastActive" ] ) >= ( time () - 900 ) ) // 900 = 15 Minutes { // User is Online } else { // User is Offline } ?> If you are doing this to show a list of Active Users MySQL can work with the Timestamp field using mathematical calculations. WHERE LastActive > DATE_ADD( NOW ( ) , INTERVAL - 15 MINUTES ) That works as far as I know, if it doesn't someone please correct me. I haven't actually used it yet! I do believe you would lose the Index Bonus that you may have set on the 0 , 1 isOnline field, but you wouldn't have to find a way to log someone off if they close the browser or go to another page. Quote Link to comment https://forums.phpfreaks.com/topic/244338-execute-script-when-window-is-closing/#findComment-1255017 Share on other sites More sharing options...
The Little Guy Posted August 9, 2011 Share Posted August 9, 2011 Run this every time the user loads a page and/or performs an action: update members set last_active = now() where member_id = 123; To get a count of the number of members active within 15 minutes, run something like this: select count(*) from members where last_active >= date_sub(now(), interval 15 minute); Quote Link to comment https://forums.phpfreaks.com/topic/244338-execute-script-when-window-is-closing/#findComment-1255030 Share on other sites More sharing options...
Jumpy09 Posted August 9, 2011 Share Posted August 9, 2011 Run this every time the user loads a page and/or performs an action: update members set last_active = now() where member_id = 123; To get a count of the number of members active within 15 minutes, run something like this: select count(*) from members where last_active >= date_sub(now(), interval 15 minute); Thank you for elaborating on that. DATE_ADD , DATE_SUB, same difference when using + or -! It is the easiest method of working with showing whether or not someone is online or offline. Quote Link to comment https://forums.phpfreaks.com/topic/244338-execute-script-when-window-is-closing/#findComment-1255031 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.