簡體   English   中英

PHP無法使用session_regenerate_id(true)登錄

[英]PHP Unable to login with session_regenerate_id(true)

我有一個用於跟蹤電話的簡單php / mysql webapp,我在索引頁面上添加了session_regenerate_id(true)時突然在生產環境中遇到問題。 之所以添加它,是因為在注銷和重新登錄之前,我注意到它保留了相同的session_id,因此我想防止會話劫持。

在index.php上,我有:

if($_SERVER['REQUEST_METHOD']!='POST') {
    session_unset();
    session_destroy(); 
    } else { 
    session_start();
    session_regenerate_id(true);
 }

我有request_method檢查,因為索引頁回發到其自身以驗證登錄嘗試。

我有一個頭文件包含在每個頁面中(index.php除外),其中包含:

<?php
    if (!isset($_SESSION)) { session_start(); }
    if(isset($_SESSION['SESSION_ID'])){
        $login = true;
    }else{
        session_destroy();
        $url = 'index.php';
        echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; 
        exit;
    }
    if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 3600)) {
    // last request was more than 60 minutes ago
    session_unset();     // unset $_SESSION variable for the run-time 
    session_destroy();   // destroy session data in storage
       $url = 'index.php?result=TimedOut';
       echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; 
       exit;
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
error_reporting(E_ALL & ~E_NOTICE);
include_once("include/config.inc");
?>

最后,在logout.php頁面上,我有以下內容:

<?php
if (!isset($_SESSION)) { session_start(); }
    if(isset($_SESSION['SESSION_ID'])){
        session_unset();
        session_destroy();
        session_write_close();
        setcookie(session_name(),'',0,'/');
        session_regenerate_id(true);
        $url = 'index.php';
        echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; 
    }else{
        session_unset();
        session_destroy();
        session_write_close();
        setcookie(session_name(),'',0,'/');
        session_regenerate_id(true);
        $url = 'index.php';
        echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; 
    }
?>

else語句中相同的會話變量用於處理某人在未啟動會話的情況下點擊logout.php的極端情況,從而防止顯示空白頁。

如上所示,我無法登錄webapp,它只是使我跳回索引頁面,但是我可以看到在服務器的/ tmp目錄中正在創建一個會話文件,其中包含session_id和我設置的其他會話變量。

UID|s:5:"jack";USER_ROLE|s:1:"2";SESSION_ID|s:32:"fd1d38794eb6c3f6a952d89bda67fd9b";

如果我注釋掉index.php中的session_regenerate_id(true),則可以登錄,但是即使注銷后,會話ID也不會改變。 為了使這一點變得更加奇怪,該代碼按我的沙箱中的方式工作,這是完全相同的代碼。 為了驗證這一點,我將生產代碼和沙箱代碼放在master分支上,並驗證了我是否在查看同一git commit。 此外,生產和開發沙箱之間的虛擬主機設置相同。

<VirtualHost xxx.xxx.xxx.xxx:80>
DocumentRoot /www/htdocs/dev.domain.com
ServerName dev.domain.com
ServerAlias dev.domain.com
ErrorLog /www/logs/dev.domain.com-error_log
CustomLog /www/logs/dev.domain.com-access_log common
RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F]
</VirtualHost>

<VirtualHost xxx.xxx.xxx.xxx:80>
DocumentRoot /www/htdocs/calllog
ServerName calllog.domain.com
ServerAlias calllog.domain.com
ErrorLog /www/logs/calllog.domain.com-error_log
CustomLog /www/logs/calllog.domain.com-access_log common
RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F]
</VirtualHost>

我有點不知所措。 有人指出我做錯了嗎?

我從來沒有發現確切的根本原因是什么。 我通過將POST移到單獨的authenticate.php頁面中而不是像我最初描述的那樣嘗試處理索引頁面中的所有內容來解決了該問題。

暫無
暫無

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

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