当用户在php中的一个打开的选项卡中注销时,会自动注销所有打开的选项卡
Logout all open tabs automatically when user logs out in one of the opened tabs in php
我正在php中研究php会话概念。使用jquery和php创建登录页面,并为所有页面创建会话。当我登录会话运行时,我可以在另一个选项卡中打开登录URL,这很好,但我在注销时遇到了问题。
当我在一个打开的浏览器选项卡中注销时,如果刷新页面被注销,它仍然会手动运行。我的要求是,当我在其中一个选项卡中注销时,其他选项卡应该自动注销,而不是手动注销。
数据库文件
<?php
session_start();
$con = mysqli_connect("localhost", "root", "","testing") or die ("Oops! Server not connected"); // Connect to the host
?>
登录.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>';
?>
创建一个php页面:
checkStatus.php
<?php
session_start();
if(isset($_SESSION['username']) && $_SESSION['username'] != '')
echo true;
else
echo false;
?>
现在每个页面都有这个jQuery代码:
var _delay = 3000;
function checkLoginStatus(){
$.get("checkStatus.php", function(data){
if(!data) {
window.location = "logout.php";
}
setTimeout(function(){ checkLoginStatus(); }, _delay);
});
}
checkLoginStatus();
因此,在一定数量的延迟之后,每个页面都会重复调用一个js函数,该函数将通过对php文件(您已经创建)进行ajax调用来检查登录状态。如果用户从任何位置注销,它将破坏浏览器中的会话,并使所有选项卡重定向到logout.php页面。
您需要有一个javascript侦听器来检查会话是否已被破坏;
window.setInterval(function(){
/// call your function here to cechk the server
}, 5000);
您可以使用ajax检查用户的会话是否仍然设置。在包含ajax 之后,您应该调用bellow-js函数
var targetURL="login.php";
function auto_check_login(){
$.ajax({
url: "check_user_session.php",
cache: false,
success: function(data){
if(data != 1){
window.location=targetURL; //Redirect user to login page.
}
}
});
}
$(document).ready(function(){
auto_check_login(); //Call auto_check_login function when DOM is Ready
});
//Call auto_check_login after 2 seconds
setInterval(auto_check_login,2000);
然后在check_user_session.php文件中,您可以拥有这个
session_start();
if( !isset($_SESSION['username']) || !isset($_SESSION['password']) ){
print 0;
} else {
print 1;
}
您必须检查$_SESSION['username']是否不仅设置了一次,而且设置了多次。检查此索引是否存在,如果不存在,则将用户重定向到登录页面。
相关文章:
- 我的单元测试选项是什么
- 如何更改bigquery API中的计费层选项
- 使用Javascript获取所选选项ID
- Selectize.js:如何对整数值的选项进行排序
- 通过js在新选项卡中有条件地打开url
- 按照选项卡索引的顺序循环一个jQuery选择
- 如何在使用selectBoxIt JQuery插件时检测选项更改
- 我们如何在互联网断开连接或用户关闭选项卡/浏览器时调用注销servlet
- 当用户关闭浏览器选项卡时,如何使用 PHP 或 javascript 从我的站点注销用户
- 使用jquery/Javascript在浏览器关闭或选项卡关闭时执行注销
- 用户关闭选项卡时注销
- 当浏览器选项卡中的 url 更改时自动注销
- 流星注销 在选项卡之间不同步
- 当用户从另一个选项卡注销谷歌时,如何执行一些javascript
- 当用户在php中的一个打开的选项卡中注销时,会自动注销所有打开的选项卡
- 如何处理在会话结束时打开多个选项卡的情况下注销用户
- 仅在浏览器或选项卡关闭时注销用户
- 如何关闭浏览器中与特定域相关的所有打开的选项卡,同时使用angular js从其中任何一个选项卡注销
- 我想在 Mozilla 或 IE 8.0 或 Chrome 中的选项卡关闭时注销
- 用户关闭一个选项卡(意味着注销)的许多选项卡,这些都没有重定向到索引