Destroy active session after browser close

Asked

Viewed 1,970 times

2

I have a system from which I need the session to be active due to an internal tool, but some users are leaving the system by directly closing the browser, without clicking the button Come out system, where the session is terminated. Is there any way the session is terminated if the user closes the system directly by the browser?

The session is created after the user logs in:

$_SESSION["Logado"] = true; 

And when you click the exit button, I destroy the session:

session_destroy();

So far all right, but if the user closes the browser? My initial reasoning was to create a time session when logging in:

$_SESSION["TempoAtivo"] = time() + 360;

But I don’t know what to do from here. I accept suggestions in jquery, javascript and PHP.

  • One solution is to use cookies, so you can set the cookie’s expiration time

  • Check out this OS reply: https://stackoverflow.com/a/24402832/4734177

  • I haven’t tried it, but maybe it works if you do an Ajax by calling a. php that destroys the session after closing the page with the function $(window).unload(function() { ajax });.

  • You can do as the friend above reported to destroir when close the browser. Maaas if the browser crashes or forces its shutdown unexpectedly the function does not perform. After I started using cookies I never wanted to know $_SESSION for login. I only use cookies, I store in the bank the key that is in the cookies with the expiration time both in the bank and in the browser. And I renew key time every time the user changes page.

  • Take a look at this later. https://stackoverflow.com/questions/8311320/how-to-change-the-session-timeout-in-php

1 answer

1


You can use the API sessionStorage Introduced in HTML5, it cleans the session locally as soon as the browser is closed. To integrate with PHP you could use the following example:

<script type="text/javascript"> 
function submitform() {
    document.getElementById("hdnLogado").value = sessionStorage.getItem('logado');
    document.myform.submit();
} </script>

And on the server side:

$_REQUEST['logado'];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.