将自动注销所有打开的选项卡

将自动注销所有打开的选项卡

本文介绍了当用户退出php中已打开的选项卡之一时,将自动注销所有打开的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究php中的php会话概念.当我登录会话运行时,使用jquery和php创建了登录页面,并为所有页面创建了会话.我可以在另一个选项卡中打开登录的url,效果很好,但注销时出现问题.

I am working on php session concept in php. created login page using jquery and php and created sessions for all pages when i logged in session runs i can open logged in urls in another tabs to which works great but i have an issue in logout.

当我在打开的浏览器选项卡之一中注销时,如果我刷新页面被注销,则其他选项卡仍然手动运行.我的要求是,当我在某个选项卡之一中注销时,其他选项卡应自动注销而不是手动注销.

when i logout in one of the opened browser tab other tabs still it runs manually if i refresh pages gets logged out. My requirement is when i logout in one of the tab other tabs should automatically logout instead of manually.

数据库文件

<?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 &raquo;" />
                    </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

checkStatus.php

 <?php
    session_start();
    if(isset($_SESSION['username']) && $_SESSION['username'] != '')
        echo true;

    else
        echo false;
?>

现在每个页面中都有以下jQuery代码:

Now in every page have this jQuery code:

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页面.

So every page after certain ammount of delay will call a js function repeatatively a which will check the login status by making an ajax call to a php file (you have created). If the user is logged out from any it will destroy the session in the browser and make all the tabs to redirect to the logout.php page.

这篇关于当用户退出php中已打开的选项卡之一时,将自动注销所有打开的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:37