I want to clear the session variables when the tab is closed but I could not find any solutions so far. here user without login they will enter the url dashboard.php means it will redirect to index.php, this condition is working fine, now user successfully login means it will go to dashboard.php page after that user close this tab and again they will enter dashboard.php page means i want to redirect the page in index.php, how can do this
<?php
session_start();
date_default_timezone_set('Asia/Kolkata');
include('dbconfig.php');
$email=$_POST['email'];
$password=$_POST['password'];
$password=md5($password);
$sql=mysql_query("SELECT id,username,email,password,is_user_type FROM login WHERE email='$email' AND password='$password'");
list($id,$username,$email,$pwd,$is_user_type)=mysql_fetch_row($sql);
if($pwd==$password){
$_SESSION['username']=$username;
$_SESSION['email']=$email;
$_SESSION['is_user_type']=$is_user_type;
$_SESSION['current'] = basename($_SERVER['PHP_SELF']);
header("Location:dashboard.php");
}
else{
echo "error";
}
?>
dashboard.php
<?php
session_start();
if(!isset($_SESSION['email']) && empty($_SESSION['email'])) {
header("Location:index.php");
}
if (isset($_SESSION['current'])) {
if (basename($_SERVER['PHP_SELF']) != $_SESSION['current']) {
session_destroy();
}
}
?>