简体   繁体   English

当用户在php中打开的一个选项卡中注销时,自动注销所有打开的选项卡

[英]Logout all open tabs automatically when user logs out in one of the opened tabs in php

I am working on php session concept in php. 我正在研究php中的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. 使用jquery和php创建登录页面,并在我登录会话运行时为所有页面创建会话我可以在另一个选项卡中打开登录的URL,但是我在注销时遇到问题。

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. 我的要求是当我在其中一个选项卡中注销时,其他选项卡应该自动注销而不是手动注销。

DB file DB文件

<?php
    session_start();
    $con = mysqli_connect("localhost", "root", "","testing") or die ("Oops! Server not connected"); // Connect to the host

?>

Login.php 的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>

welcome home page 欢迎主页

<?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 logout.php

<?php
    include 'library.php';
    session_destroy();
    unset($_SESSION['username']);
    unset($_SESSION['password']);
    echo '<script type="text/javascript">window.location = "login.php"; </script>';

?>

Create a php page: 创建一个php页面:

checkStatus.php checkStatus.php

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

    else 
        echo false;
?>

Now in every page have this jQuery code: 现在每个页面都有这个jQuery代码:

var _delay = 3000;
function checkLoginStatus(){
     $.get("checkStatus.php", function(data){
        if(!data) {
            window.location = "logout.php"; 
        }
        setTimeout(function(){  checkLoginStatus(); }, _delay); 
        });
}
checkLoginStatus();

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). 因此,在某些延迟之后的每个页面都将重复调用一个js函数,它将通过对php文件(您已创建)进行ajax调用来检查登录状态。 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. 如果用户从任何用户注销,它将在浏览器中销毁会话并使所有选项卡重定向到logout.php页面。

You need to have a javascript listener that checks if the session has been destroyed; 您需要有一个javascript侦听器来检查会话是否已被销毁;

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

You can use ajax to check if the user's session is still set. 您可以使用ajax检查用户的会话是否仍然设置。 You should make a call to the bellow js function after you have included 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);

Then in check_user_session.php file, you can have this 然后在check_user_session.php文件中,你可以拥有它

session_start();

if( !isset($_SESSION['username']) || !isset($_SESSION['password']) ){
print 0;
} else {
print 1;
}

You have to check if $_SESSION['username'] is set not just one time, but many times. 您必须检查$ _SESSION ['username']是否设置了一次,但不是多次。 Check if this index exists and, if not, redirect the user to the login page. 检查此索引是否存在,如果不存在,则将用户重定向到登录页面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户注销其中一个选项卡时,如何自动从所有打开的选项卡中注销用户? - How can I logout a user from all open tabs automatically when user logs out in one of them? 当用户注销其中一个选项卡时自动注销所有打开的选项卡 - Logout all open tabs automatically when user logs out in one of them 从所有活动选项卡注销用户 - logout user from all active tabs 如何使用 php 或 javascript 在所有浏览器中获取当前打开的选项卡的 url? - How to get the url of currently opened tabs in all browsers with php or javascript? php javascript 在浏览器中更新站点的所有打开选项卡中的 div - php javascript update a div in all opened tabs of a site in browser 如何在使用angular js从任何一个选项卡注销时关闭与特定域相关的所有浏览器打开的选项卡 - How to close browser all open tabs related to a specific domain while logout from any of those one tab using angular js 当在 React.js 中注销一个选项卡时,如何在一个浏览器中注销所有选项卡? - How to log out all tabs in one browser when one tab is logged out in React.js? 如何在打开时删除其他选项卡 - How to remove other tabs when one is open 当会话结束时,您如何处理在打开多个选项卡的情况下注销用户? - How do you deal with logging out a user with multiple tabs open when a session ends? 如何使用批处理文件自2分钟以来打开的脚本来自动关闭IE的选项卡,… - how to close tabs of the IE one by one automatically using scripts which are opened since 2 mins using batch file, …
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM