2012-08-28 51 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.  
}