Welcome Guest, Not a member yet? Register   Sign In
Delete session from DB when closing browser
#1

[eluser]cmh24[/eluser]
Hi everybody,

I want to avoid multiple logins at the same time (meaning not 2 computers can be logged in with the same credentials).

I managed to insert the user_id in the ci_session table with the help of a small overriding of sess_write. So far login and logout are working, everytime the user_id field gets updated (either the specific user_id or NULL).

Now when somebody logs in, I want to query the ci_session table for the specific user_id and then proceed or not.

But when the user just closed the browser without logging out before, the session entry stays in the table and this user can not log in again.

I use 'sess_expire_on_close' and set it to TRUE.

I hope anybody has an idea.

Best wishes!
Max
#2

[eluser]InsiteFX[/eluser]
Maybe this will help you.

Detect browser close event
#3

[eluser]cmh24[/eluser]
First of all, thanks for your hint.

This is what I came up with
Code:
$(document).ready(function() {
<?php if($this->session->userdata('logged_in') == 1):?>

<?php $group_name= $this->session->userdata('group_name');?>
<?php $url = base_url().$group_name.'/settings/logout';?>

$(window).on('unload', function(){
  $.ajax({
   type : "POST",
   url : "<?php echo $url;?>",
   success: function()
   {
    alert('Success');  
   }
  });
});
<?php endif;?>
});

I did some research and found this.

The unload event is triggered when:

- a link to leave the page is clicked
- a new URL is typed in the address bar
- the forward or back buttons are used
- the browser window is closed
- the page is reloaded

Now I only want to trigger the event when the browser windows is closed.
So, what can I do?

Greetings!
Max
#4

[eluser]CroNiX[/eluser]
There isn't a way to detect that afaik.
#5

[eluser]Tim Post[/eluser]
[quote author="MaxH24" date="1337937078"]
I did some research and found this.

The unload event is triggered when:

- a link to leave the page is clicked
- a new URL is typed in the address bar
- the forward or back buttons are used
- the browser window is closed
- the page is reloaded

Now I only want to trigger the event when the browser windows is closed.
So, what can I do?
[/quote]

The reason why the window was closed is not disclosed to the event, you only get notification that it happened. It's meant to allow you to prompt the user to finish something that isn't saved prior to deliberately telling your app 'I don't want to save this'.

To get what you want, just maintain a separate table of active users, and use setInterval() in the client to keep it fresh. If a user attempts to login after just closing the browser, they'll only be told that they are already logged in for .. 30 seconds or so (or whatever frequency you use with setInterval()).

I use Redis for this to avoid query overhead, and a simple getJSON() request to keep it simple. It's also convenient to tell the user 'hey, looks like you've lost internet connectivity' if getJSON fails (and possibly do other work like stop websockets / other longpolls).




Theme © iAndrew 2016 - Forum software by © MyBB