douhuan1380
douhuan1380
2016-12-03 13:03
浏览 51

浏览器关闭时如何关闭会话并重定向到索引页面

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();
 }
 }
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dounangshen6553
    dounangshen6553 2016-12-03 13:17

    First, your xyz.php file set a unique variable in the $_SESSION array which will act as a mark that the user has been on this page

    $_SESSION['previous'] = basename($_SERVER['PHP_SELF']);
    

    Then, add the following code on all pages, before any output to check if the user is coming from xyz.php

    if (isset($_SESSION['previous'])) {
       if (basename($_SERVER['PHP_SELF']) != $_SESSION['previous']) {
        session_destroy();
        unset($_SESSION['previous']);
       }
    }
    
    点赞 评论
  • dongtu4028
    dongtu4028 2016-12-03 13:42

    To remove particular session data , try this

      if($_SESSION[sessionvaribale] )
      {
       unset($_SESSION[sessionvaribale]);
       }
    

    To destroy all session data - try session_destroy()

    Its already discussed by Stackoverflow

    Refer Session destroy when logout

    点赞 评论

相关推荐