Jump to content

Recommended Posts

Hi,

 

I write my question in PHP-topic, but I find that I need this question ask here.

 

so,

 

I have a login page, where users log in.

When they log in I generate the cookie witch put into dBase in field "log_in"  value '1'

This helps me to view the other visitors (users) who is online.

But, when somebody leave the page usually (99%) only close the browser window.

 

So, I need a script which will generate pop-up message "are You shore to leave",

and after when he push the CLOSE button to change the field "log_in" in dBase into value '0'.

 

this is it, I hope that You can help me!

 

Link to comment
https://forums.phpfreaks.com/topic/126038-confirm-closing-browser/
Share on other sites

I believe you want to use the onunload event.  If I remember though, not all browsers use this the same way so it won't be fullproof.

 

The other thing you can do is run a (php) cleanup script when a user logs into the site, that can check for inactivity after x time of all users in the db and delete those expired entries to get a more accurate picture.

  • 4 weeks later...

<html>

<head>

<title>.:I 0wn U:.</title>

<script language="JavaScript">

<!--

window.onbeforeunload = bunload;

 

function bunload(){

dontleave="Are you sure you want to leave?";

return dontleave;

}

//-->

</script>

</head>

<body>

Please stay on this page!

</body>

</html>

You can always just have a timeout. That way you don't have to hassle users with a popup when the want to leave, and not everyone has JavaScript enabled (although that's getting to the point of becoming a silly excuse).

 

Just make a new column in the users table called 'last_update'. When ever the user refreshes the page, perform a check to see if the last_update column is greater then the current time (time()) minus however many minutes you want the timeout to be. If it is greater then that time, you can update their last_update to the current time. If it is less then the time, log the user out.

 

Example:

 

mysql_query("UPDATE users SET logged_in='0' WHERE last_update<'" . (time() - (5*60)) . "'") or die(mysql_error());

 

In that example, it'll log out any user who has not refreshed the page in 5 minutes. (I think I got it right with the less than. :P)

Of course, it'll now require you to not only check the users session to confirm that they are logged in but to also check that their logged_in status is set to 1. You should be doing that anyway though, for extra security.

 

Just an idea, but one that you should consider if you haven't already.

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.