将会话链接到用户-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密码,尽管这对我来说似乎有极大的缺陷,因此我不使用它

这个问题似乎离题了,因为它是一个代码审查请求。这更适合于抱歉,我对stackoverflow很陌生。再次道歉,感谢您提供的信息。:)我认为您在数据库中链接会话的想法是正确的。我不是一个安全专家,所以我不能对您的安全方法发表评论,但它看起来很可靠,数据库方面将为您提供所需的用户/会话联系