Exit all open tabs automatically when user goes to one of open tabs in php

I am working on php session concept in php. created a login page using jquery and php and created sessions for all pages, when I logged in to the session, I can open the registered URLs on other tabs for which it works fine, but I have a problem logging out.

when I go to one of the open tabs of the browser to the other tabs, it is still done manually if I refresh the pages that are logging out. My requirement is when I go to one of the tabs, the other tabs should automatically log out, not 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>'; ?> 
+5
source share
4 answers

Create a php page:

checkStatus.php

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

Now every page has this jQuery code:

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

Thus, each page after a certain amount of delay will call the js function, repeating, which will check the login status by making an ajax call to the php file (you created). If the user logs out, he will destroy the session in the browser and make all the tabs redirected to the logout.php page.

+9
source

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

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

You can use ajax to check if a user session is established . You must make jellow function call after you enable ajax

 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 you can have this

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

You should check if $ _SESSION ['username'] is set more than once. Check if this index exists and, if not, redirects the user to the login page.

0
source

Source: https://habr.com/ru/post/1235867/


All Articles