2015-11-13 65 views
5

中打開的選項卡中註銷時,我正在使用php中的php會話概念。創建登錄頁面使用jQuery和PHP和創建會話的所有頁面時,我登錄會話運行我可以在另一個標籤中打開登錄的URL在其中工作很好,但我有一個問題在註銷。註銷所有打開的選項卡,當用戶在php

當我註銷其中一個打開的瀏覽器選項卡時,其他選項卡仍然會手動運行,如果我刷新頁面會被註銷。我的要求是,當我註銷其中一個選項卡的其他選項卡應自動註銷而不是手動。

DB文件

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

歡迎主頁

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

?> 
+0

http://stackoverflow.com/questions/13513874/logout-all-open-tabs-automatically-when-user-logs-out-in-one-of-them – rocky

+0

謝謝@rocky。 – Sjay

+0

[當用戶註銷其中一個時自動註銷所有打開的選項卡]的可能重複(http://stackoverflow.com/questions/13513874/logout-all-open-tabs-automatically-when-user-logs-out-其中一個) –

回答

9

創建一個PHP頁面:在每一頁

checkStatus.php

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

    else 
     echo false; 
?> 

現在有這樣的jQuery代碼:

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

所以延遲的某些ammount的後每一頁將重複調用一個js函數,它將通過對php文件進行ajax調用來檢查登錄狀態(你h大街創建)。如果用戶從任何地方註銷,它將破壞瀏覽器中的會話,並使所有選項卡重定向到logout.php頁面。

+0

謝謝@void .. – Sjay

+0

很高興幫助,如果它解決您的問題善意考慮它投票並標記爲一個正確的答案。 – void

3

你需要有一個JavaScript監聽器來檢查,如果會話已被破壞;

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

謝謝@ Yayo88。 – Sjay

0

您可以使用ajax來檢查用戶的會話是否仍然設置。 你應該到波紋管的js函數的調用已包含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); 

後,然後在check_user_session.php文件,你可以有這個

session_start(); 

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

你必須檢查是否$ _SESSION ['username']不僅僅是一次,而是多次。 檢查該索引是否存在,如果不存在,則將用戶重定向到登錄頁面。

相關問題