簡體   English   中英

如何處理PHP會話超時

[英]How to deal with a PHP session timeout

我有一個顯示時間表的網頁區域,用戶可以左右單擊以前進/后一周:

<script type="text/javascript">
 var TimesheetOffset = 0;
 function TimesheetNav(AdjustBy) {
   TimesheetOffset = TimesheetOffset + AdjustBy;
   $("#timesheets").load("populate_timesheets.php?start=" + TimesheetOffset);
 }
</script>

populate_timesheets.php腳本在ID為“ timesheets”的DIV區域中顯示時間表信息,但僅在用戶登錄且其會話尚未過期時才顯示-如果會話已過期,則顯示登錄頁面:

    if (!isset($_SESSION['login_user'])) {
        header("location: index.php");
    }

問題是,如果會話已超時,則會在應顯示時間表的位置出現index.php腳本,而不是替換整個頁面。 如何更改PHP或JavaScript,以便如果會話超時,登錄頁面將替換整個頁面?

JavaScript代碼:

$.ajax("populate_timesheets.php?start=" + TimesheetOffset, {
   type: "GET",
   statusCode: {
      401: function (response) {
         // redirect to login page
      }
   }, 
   success: function (data) {
      $( "#timesheets" ).html(data);
   },
   error: function (error) {
      console.log('Error occured: ', error);
   }
});

PHP代碼:

if (!isset($_SESSION['login_user'])) {
    header("HTTP/1.1 401 Unauthorized");
    return;
}

我不測試此代碼,因此,如果您有問題,請閱讀有關AJAX請求的jQuery文檔:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM