我已经阅读了很多有关php安全最佳实践的内容,我正努力在我的xampp服务器上使用它们。
我有一个包含我的所有安全性,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 :(得分:5)
删除session_regenerate_id(true);
这是不必要的,不会覆盖以前的cookie,但“真实”是真正的问题,因为它清除了以前的会话细节。
答案 1 :(得分:1)
看看你正在设置的cookie(s!)。我在同一个函数中遇到了同样的问题,并通过在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.
}