Log out when browser close or session ends [duplicate]

2

I'm developing a system that stores in the database when user logs in and out. However, the logout function is only called when the user clicks exit.

I would like to know if you can call the logout function when the browser closes or the session ends.

    
asked by anonymous 16.10.2018 / 21:00

1 answer

1

You could try using the 'onunload' function

<body onunload="funcaoEventoSair()">

<script>
function funcaoEventoSair() {
    alert("SAIR"); // colocar sua lógica de log off aqui 
}
</script>

another alternative:

<script>
window.onbeforeunload = function(){
        alert("SAIR"); // colocar sua lógica de log off aqui 
}
</script>
    
16.10.2018 / 21:23