Jump to content

execute script when window is closing


manix

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/244338-execute-script-when-window-is-closing/
Share on other sites

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.

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.

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);

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.