将会话链接到用户-PHP
我目前正试图找到将我生成的会话链接到数据库用户的最佳安全方法。当用户使用有效凭据登录时,会生成一个会话将会话链接到用户-PHP,php,mysql,session,Php,Mysql,Session,我目前正试图找到将我生成的会话链接到数据库用户的最佳安全方法。当用户使用有效凭据登录时,会生成一个会话 function sessionStart(){ ini_set('session.use_only_cookies', 1); // Forces sessions to only use cookies. ini_set('session.entropy_file', '/dev/urandom'); // better session id's, more random
function sessionStart(){
ini_set('session.use_only_cookies', 1); // Forces sessions to only use cookies.
ini_set('session.entropy_file', '/dev/urandom'); // better session id's, more random than PHP's default.
ini_set('session.entropy_length', '512'); // How many bytes which will be read from the above file. 512 = overkill?
ini_set('session.hash_function','sha512');
ini_set('session.hash_bits_per_character','6');
$session_name = 'sec_login'; // Sets a custom session name.
$secure = false; // Set to true if HTTPS is being used.
$httponly = true; // Stops Javascript from being able to read the 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();
session_regenerate_id(); // regenerated the session, delete the old one.
}
理论上,这会生成一个唯一、安全的id,可用于识别用户。我唯一的问题是如何将生成的会话链接到用户
我假设最好的方法是创建一个包含两列(userId和SessionId)的数据库表,在创建会话时,该表填充适当的userId和SessionId,在用户重新加载不同页面时更新,或者在用户注销时删除
- 这安全吗李>
- 有更好的方法吗
- 我注意到有人使用的一种方法是在cookie中包含一个hash+salt密码,尽管这对我来说似乎有极大的缺陷,因此我不使用它