在用户注销一个打开的选项卡时,自动注销所有打开的选项卡(PHP实现)

5

我正在研究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 &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>';

?>

https://dev59.com/32Yr5IYBdhLWcg3wpLr9 - rocky
可能是[当用户注销其中一个选项卡时自动注销所有打开的选项卡]的重复问题(https://dev59.com/32Yr5IYBdhLWcg3wpLr9) - Vivek Nerle
4个回答

10
创建一个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();

所以,在一定延迟后,每个页面都将重复调用一个名为a的js函数,该函数将通过向一个php文件(您已经创建)发出ajax调用来检查登录状态。如果用户从任何地方退出了,则会在浏览器中销毁会话并使所有标签页重定向到logout.php页面。


3

您需要有一个JavaScript监听器来检查会话是否已被销毁;

window.setInterval(function(){
  /// call your function here to cechk the server
}, 5000);

0

你可以使用ajax来检查用户会话是否已设置。

在包含ajax之后,应该调用下面的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;
}

0
你需要多次检查 $_SESSION['username'] 是否已设置。检查该索引是否存在,如果不存在,则将用户重定向到登录页面。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接