EllisLab text mark
Advanced Search
     
Callback function on browser close
Posted: 03 July 2012 07:30 AM   [ Ignore ]
Joined: 2012-02-10
8 posts

Hi to all,

I have a request from client to catch browser closing and log in DB table user id and session lenght.

My CI version is 2.1.1 that has in config sess_expire_on_close, but in system/libraries/Session.php variable sess_expire_on_close is just mentioned as parameter and I can’t find any further use in file.

Can someone guide me how to catch browser closing and bind callback to it ?

 
Posted: 03 July 2012 04:41 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-05-17
1415 posts
Hallas - 03 July 2012 07:30 AM

Hi to all,

I have a request from client to catch browser closing and log in DB table user id and session lenght.

My CI version is 2.1.1 that has in config sess_expire_on_close, but in system/libraries/Session.php variable sess_expire_on_close is just mentioned as parameter and I can’t find any further use in file.

Can someone guide me how to catch browser closing and bind callback to it ?

Not possible. While you can warn a person that they are navigating away from the page and may lose what they were working on, there’s no way to do what your client is asking.

You might search for onbeforeunload, which is a javascript method. This is as close as you can get to an event for browser close, but it’s going to give the site visitor a confirmation, and they can click yes or cancel.

A solution might be to implement some javascript that pings the server with the user’s ID every 30 or 60 seconds, however, this might be expensive if you have a lot of users.

 Signature 

Brian
Brian’s Web Design - Temecula
Community Auth - CodeIgniter Authentication Application

 
Posted: 03 July 2012 05:06 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

In your terms it would make a difference, if the user just browsed away from your page e.g. to go to Google.com or if he closed the browser window.

However, you cannot detect the second one especially not in the case when the user is no longer on your side. What you can however do is to make your app fully AJAX based which allows you to use techniques used by GoogleMail, DokuWiki and FacebookMessage which will alert the user that he is moving away from a page (you know the prompt asking you whether want to stay or leave the current page).

That’s for my knowledge the only possibility to do what you want on browser close.

But, I just had another idea: If the page views aren’t for long times (i.e. longer than e.g. 5 seconds) you could just update the session-lifetime each time a new page is requested. This way you will actually always loose the last page or the time the user spent on the last page viewed respectively, but you can get an estimate of how long every user was browsing a page.

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 03 July 2012 05:07 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4420 posts

Also know that things like reload, Ctrl+R, F5 etc will trigger the onbeforeunload event, while the user isn’t actually leaving the page. So making that reliable might prove to be a challenge.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM