3

私はPHPセキュリティのベストプラクティスについて多くのことを読んでおり、xamppサーバーでそれらを採用するために一生懸命努力しています.

すべてのセキュリティ、ddos、セッション管理を行うインクルードがあり、その中に sec_session_start という関数があります。以下にコードを示しますが、ログインしようとしてホームページにリダイレクトすると、すべてのセッション データが失われます。私のログイン プロセス ページでは、リダイレクトを行う前に、正しいセッション データがすべて表示されています。

各ヘッダーの後、「exit;」を実行しています。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.  
}

この関数は、すべてのページで呼び出されます。

助言がありますか?

4

2 に答える 2

5

session_regenerate_id(true);を削除します。

これは不要であり、以前のCookieを上書きしませんが、「true」は以前のセッションの詳細を消去するための実際の問題です。

于 2012-08-28T03:59:24.500 に答える
1

設定している Cookie を見てください。同じ関数で同じ問題が発生し、session_set_cookie_params() でドメインを明示的に指定して修正しました。何らかの理由で、www.example.com と example.com の両方の Cookie が設定されていました。

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.     
}
于 2013-04-23T20:46:21.633 に答える