yosa
yosa

Reputation: 11

how to make session ends after closing browser

i used a simple session to create a login system,

<?php
    session_start();
    $_SESSION['username'] = $username;
?>

this is just a part of the code but it is what generate my session , it works fine until now, the problem is that this session don't end with browser closure, i tryed to login the close the browser , but when i visit the website again i find that i'm still logged in, the only way to logout is using a log out system, what i want is an automatic way to close the session on browser's closure, thanks in advance

Upvotes: 1

Views: 833

Answers (1)

Tushar
Tushar

Reputation: 8049

Simple Answer: You can't. The session exists on the server and the browser exists on the client's machine. The browser does not notify the server when it is closing. In many cases (ex: a computer failure or network disconnect), the browser is unable to notify your server even if it wanted to.

Complex Answer: You could set a very low session timeout time (ie: 5 minutes) and update it every time the user pings the server, but this will log people out if they've been away from their keyboard for more than your timeout time.

Upvotes: 3

Related Questions