我有一个登录脚本,当用户转到另一个站点或返回登录页面时,该脚本不会关闭会话。我的问题是,当他们离开网站或目录之外时,如何销毁会话?我需要在用户启动会话时添加超时参数吗?我需要使用cookie而不是会话吗?
的login.php
require("../includes/header.php");
if($_SERVER["REQUEST_METHOD"] == "POST"){
$p_num = $_POST["username"];
$pwd = $_POST["password"];
$query = "SELECT * FROM $user_table";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($result)){
$user_id = "{$row['user_id']}";
$user_name = "{$row['user_name']}";
$password = "{$row['password']}";
$image = "{$row['image']}";
$email = "{$row['email']}";
$program = "{$row['program']}";
$role = "{$row['role']}";
if(($user_id == $p_num) && ($pwd == $password)){
$_SESSION["id"] = $user_id;
$_SESSION["user"] = $user_name;
$_SESSION["program"] = $program;
$_SESSION["pass"] = $password;
$_SESSION["image"] = $image;
$_SESSION["email"] = $email;
$_SESSION["role"] = $role;
header("Location: ../pages/instructor.php");
}
else{
header("Refresh: 1; URL=../index.php");
}
}
}
instructor.php
<?php require("../includes/header.php"); ?>
<title></title>
<link href="../css/style.css" rel="stylesheet/less" type="text/css">
<script src="../js/jquery.2.0.3.js"></script>
<script src="../js/script.js"></script>
<script src="../js/less-1.7.4.min.js"></script>
</head>
<body>
<div id="page">
<header>
<div id="logo" class="logo_bg"></div>
<div id="fsi_logo" class="logo_bg"></div>
</header>
<div id="main">
<div id="instructor">
<?php
echo "<img id=instructor_image src=" .$_SESSION["image"] .">";
echo "<h1>" .$_SESSION["user"] ."</h1>";
echo "<span><p>" .$_SESSION["program"] ."</p> - <h2>" .$_SESSION["role"] ."</h2></span>";
echo "<a href=mailto:" .$_SESSION["email"] .">" .$_SESSION["email"] ."</a>";
?>
</div>
<div id="bleg">
<h1><a href="../pages/create_scenario.html">BUILD SCENARIO</a></h1>
<h1><a href="#">SEARCH SCENARIOS</a></h1>
<h1><a href="#">VIEW SCENARIOS</a></h1>
</div>
<?php require("../includes/footer.html"); ?>
logout.php
session_start();
session_unset();
session_destroy();
的script.js
$(window).on('beforeunload', function(e){
e.preventDefault();
ajax = new XMLHttpRequest();
ajax.open("../php/logout.php", "POST", true);
ajax.send();
})
答案 0 :(得分:1)
使用javascript
window.onbeforeunload = function (e) {
e.preventDefault(); //Not even sure what the default action does, but oh well
ajax = new XMLHttpRequest();
ajax.open("killsession.php","POST",true);
ajax.send();
}
killsession.php当然会被会话杀死
答案 1 :(得分:0)
为unload doc编写一个jquery / JS事件处理程序,并向ExpireSession.php发送一个请求到期会话
答案 2 :(得分:0)
正如所说&#34;这取决于什么构成&#34;离开&#34;&#34;
$count = $_SESSION['count'];
if($count === 1) {
unset ($_SESSION['count']);
}
else (empty($_SESSION['count'])) {
$_SESSION['count'] = 1;
}