Log off a user when they leave web site


I would like to log off a user if they leave my site.

I have tried a few things such as ....

        window.onbeforeunload = function(){
                type: "POST",
                //page and method
                url: "../KeepSessionAlive.aspx/logOutUser",
                async: false,
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                    error: function (xhr, ajaxOptions, thrownError) {

However this logs off the user if they leave the web page for another web page. I only want to invoke a log off if they actually navigate to another site altogether.

Would be grateful for any help.

Who is Participating?
GaryConnect With a Mentor Commented:
Then you need to set a last access time as part of the profile db table and for example if it is more than 5 mins since they last accessed a page then they need to login again
There is no reliable way to detect it.
And even when it does work your alerts would not fire. There is only one option for the alert and that is 'Do you want to leave this website' - Yes/No (and the yes/no part you cannot change - or is it OK/Cancel - cannot remember)
Not going to happen. The browser does not know it is leaving the site until it has already left.  

Why is it you think you ned to know when they leave the site?

SolugaAuthor Commented:
Because if they leave the site and do not log off the web site then another user could potentially access the system by going back in history.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.