Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当用户注销php中打开的某个选项卡时,自动注销所有打开的选项卡_Javascript_Php_Jquery_Session_Tabs - Fatal编程技术网

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

Javascript 当用户注销php中打开的某个选项卡时,自动注销所有打开的选项卡,javascript,php,jquery,session,tabs,Javascript,Php,Jquery,Session,Tabs,我正在用php编写php会话概念。使用jquery和php创建登录页面,并为所有页面创建会话当我登录会话运行时,我可以在另一个选项卡中打开登录的URL,这很好,但我在注销时遇到了一个问题 当我在一个打开的浏览器选项卡和其他选项卡中注销时,如果我刷新页面注销,它仍然会手动运行。我的要求是,当我在其中一个选项卡中注销时,其他选项卡应自动注销,而不是手动注销 DB文件 <?php session_start(); $con = mysqli_connect("localhost"

我正在用php编写php会话概念。使用jquery和php创建登录页面,并为所有页面创建会话当我登录会话运行时,我可以在另一个选项卡中打开登录的URL,这很好,但我在注销时遇到了一个问题

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

DB文件

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

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

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 'library.php';
    session_destroy();
    unset($_SESSION['username']);
    unset($_SESSION['password']);
    echo '<script type="text/javascript">window.location = "login.php"; </script>';

?>

用户名
密码
欢迎主页

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

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

?>

您需要一个javascript侦听器来检查会话是否已被破坏

window.setInterval(function(){
  /// call your function here to cechk the server
}, 5000);
创建一个php页面:

checkStatus.php

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

    else 
        echo false;
?>
因此,每个页面在经过一定的延迟后,都会重复调用一个js函数,该函数将通过对php文件(您已经创建)进行ajax调用来检查登录状态。如果用户从任何位置注销,它将破坏浏览器中的会话,并使所有选项卡重定向到logout.php页面。

您可以使用来检查用户的会话是否仍然设置。 您应该在包含之后调用bellow js函数

然后在检查\u user\u session.php文件中,您可以使用

session_start();

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

您必须检查$\u SESSION['username']是否不只设置了一次,而是多次设置。
检查此索引是否存在,如果不存在,请将用户重定向到登录页面。

很高兴它能帮助您解决问题,请考虑将其投票并将其标记为正确答案。可能重复