我正在研究PHP中的会话概念。使用jQuery和PHP创建了登录页面,并在登录时为所有页面创建了会话。当我登录时,会话正常运行,我可以在另一个标签页中打开已登录的URL,这很好,但是我在注销时遇到了问题。
当我在一个已打开的浏览器标签页中注销时,其他标签页仍然保持登录状态,如果我手动刷新页面,就会被注销。我的需求是,当我在其中一个标签页中注销时,其他标签页应该自动注销,而不是手动注销。
数据库文件
<?php
session_start();
$con = mysqli_connect("localhost", "root", "","testing") or die ("Oops! Server not connected"); // Connect to the host
?>
Login.php
<?php
include 'db.php';
if(isset($_SESSION['username']) && $_SESSION['username'] != '')
{ // Redirect to secured user page if user logged in
echo '<script type="text/javascript">window.location = "userpage.php"; </script>';
}
?>
<html>
<body>
<form>
<table class="mytable">
<tr>
<td>Username</td>
<td>
<input type="text" name="username" id="username" class="as_input" value="s"/>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" name="password" id="password" class="as_input" value="s"/>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="login" id="login" class="as_button" value="Login »" />
</td>
</tr>
</table>
</form>
</body>
</html>
欢迎访问主页
<?php
include 'db.php';
if(!isset($_SESSION['username']) || $_SESSION['username'] == '')
{
echo '<script type="text/javascript">window.location = "login.php"; </script>';
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="as_wrapper">
<h2>
welcome to home page
</h2>
<a href="logout.php" class="a">logout</a><br><br>
<a href='#'>test link</a>
</div>
</body>
</html>
logout.php
<?php
include 'library.php';
session_destroy();
unset($_SESSION['username']);
unset($_SESSION['password']);
echo '<script type="text/javascript">window.location = "login.php"; </script>';
?>