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

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

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 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']);
       }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?