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:


  <?php
include 'config.php';
  
  session_start();

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

and logout page


<?php 
session_start();
session_destroy();

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