Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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>';

?>
like image 442
Sjay Avatar asked Nov 13 '15 09:11

Sjay


People also ask

How user will logout from all open tabs automatically when user logs out in one of them in php?

localStorage. setItem('logout-event', 'logout' + Math. random()); Every other tab will get it with a listener.

How do you logout from all tabs in react JS?

There is a neat little way to log out all the tabs if the user clicks logout from one tab. Since we will be maintaining a token in localStorage. We can add an event listener for our storage and check if the token becomes null then, we'll dispatch a logout action to logout the user from all the open tabs.


2 Answers

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.

like image 151
void Avatar answered Oct 17 '22 15:10

void


You need to have a javascript listener that checks if the session has been destroyed;

window.setInterval(function(){
  /// call your function here to cechk the server
}, 5000);
like image 37
CiaranSynnott Avatar answered Oct 17 '22 14:10

CiaranSynnott