Is it possible to use JavaScript onUnload function to call a PHP script that triggers session_destroy()?
The issue can be solved by using an AJAX request in the JavaScript onUnload function to call a PHP script that triggers the session_destroy() function. This will ensure that the session is properly destroyed when the user navigates away from the page.
<?php
session_start();
// Check if session is active
if (isset($_SESSION['user'])) {
// Unset all session variables
$_SESSION = array();
// Destroy the session
session_destroy();
}
?>