2012-08-28 50 views
3

我已經閱讀了很多關於php安全性的最佳實踐,所以我努力在我的xampp服務器上使用這些最佳實踐。我的php會話在登錄後不斷丟失

我有一個包含所有我的安全性,ddos,會話管理,並有一個名爲sec_session_start的函數。代碼如下,但當我嘗試登錄,然後重定向回到我的主頁時,所有會話數據都消失了。在我的登錄進程頁面上,在我進行重定向之前,它擁有所有正確的會話數據。

每個標題後,我正在做「退出」。我也試過寫session_write_close();

但這並不能解決我的問題。

以下是功能碼。

function sec_session_start() { 
$session_name = 'AnyName'; // Set a custom session name 
$secure = false; // Set to true if using https. 
$httponly = true; // This stops javascript being able to access the session id. 

ini_set('session.use_only_cookies', 1); // Forces sessions to only use cookies. 
$cookieParams = session_get_cookie_params(); // Gets current cookies params. 
session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly); 
session_name($session_name); // Sets the session name to the one set above. 
session_start(); // Start the php session 
session_regenerate_id(true); // regenerated the session, delete the old one. 
} 

這個函數在每一頁都被調用。

有什麼建議嗎?

+0

這是包含每個頁面調用get的代碼。 http://pastebin.com/e5r4Uvvu – crosenblum

回答

4

刪除session_regenerate_id(true);

這是不必要的,不會覆蓋以前的cookie,但是「真實」是真正的問題,因爲它清除了以前的會話詳細信息。

+0

我放棄了它,當我成功登錄到主頁後重定向,並且我有會話轉儲時,它顯示沒有值。 – crosenblum

+0

試着刪除整個函數,除了「session_start」。剩下的就是矯枉過正,特別是在嘗試調試時。保持會話名稱,如果你有,但不要,如果你已經這樣做「,因爲」 – Robbie

+0

公平不夠,只是希望這是安全的儘可能。 – crosenblum

1

看看你正在設置的cookie(s!)。我對同一個函數有同樣的問題,並通過在session_set_cookie_params()中明確聲明我的域來修復它。由於某種原因,www.example.com和example.com的cookies都被設置了。

有關session_regenerate_id(true)的註釋看起來像是一個紅色鯡魚,因爲它應該複製任何現有的會話變量......並且它也可以工作。

function sec_session_start() { 
    $domain = 'example.com'; // note $domain 
    $session_name = 'sec_session_id'; // Set a custom session name 
    $secure = true; // Set to true if using https. 
    $httponly = true; // This stops javascript being able to access the session id. 
    ini_set('session.use_only_cookies', 1); // Forces sessions to only use cookies. 
    $cookieParams = session_get_cookie_params(); // Gets current cookies params. 
    session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $domain, $secure, $httponly); // note $domain 
    session_name($session_name); // Sets the session name to the one set above. 
    session_start(); // Start the php session 
    session_regenerate_id(true); // regenerated the session, delete the old one.  
}