Auto logout if the brwoser is close

Good day!

i have simpe login page with logout…

But, I need to have a syntax for autologout when the browser is closed and when he open again the login page he need to login again…

How can it possible?

Here is my login code:

include 'config.php';

  if (isset($_POST['submit'])) {
     $sql = "SELECT username, password FROM paylogin WHERE username='$username' AND password='$password'";
     $rs = $conn2->Execute($sql);
        $_SESSION['logged_in'] = true;
    else {
    echo "<center>";
    echo "Wrong Username or Password";
    echo "</center>";

and logout page


header ("Location: login.php");

Thank you

If the user closes their browser, the session should expire unless you have it set otherwise.

Otherwise the only real answer is to use Javascript with an AJAX call on the onbeforeunload event. But that is not 100% reliable and so not a very good option.

The other option is to just destroy the session if it is more than X minutes old.

what shoud i do in my code session_destroy(); in my logout page? to work the autologout?

Thank you so much