普通网友 2015-11-13 09:49
浏览 51
已采纳

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

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.

DB file

<?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>

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

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

?>
  • 写回答

4条回答 默认 最新

  • donglu1971 2015-11-13 09:54
    关注

    Create a php page:

    checkStatus.php

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

    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();
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题