The following code will only be called when a session has timed out. I basically want to display a confirm dialog with javascript to make the user pick if he wants to remain online or not. My problem is that the php that closes the session inside the javascript seems to be executed even if the confirm is true.
if (isset($_SESSION['username']) && isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > 5)) {
?>
<script type="text/javascript">
if (confirm("You were logged off due to inactivity, if you would like to remain logged in click OK.")) {
header("Location:/");
} else {
<?php
connect();
if(!connect()) {
die('Could not connect: ' . mysql_error());
header( "refresh:3; url=/index.php" );
}
mysql_select_db('www');
$user = $_SESSION['username'];
mysql_query("UPDATE users SET online='0'
WHERE username='$user'");
session_unset();
session_destroy();
?>
}
</script>
<?php
}
$_SESSION['last_activity'] = time(); // update last activity time stamp
?>