首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在PHP中正确防止会话劫持

在PHP中正确防止会话劫持
EN

Stack Overflow用户
提问于 2011-12-08 01:02:35
回答 1查看 10K关注 0票数 22

我知道这个话题已经讨论了很多,但我还有几个具体的问题没有得到回答。例如:

代码语言:javascript
复制
// **PREVENTING SESSION HIJACKING**
// Prevents javascript XSS attacks aimed to steal the session ID
ini_set('session.cookie_httponly', 1);

// Adds entropy into the randomization of the session ID, as PHP's random number
// generator has some known flaws
ini_set('session.entropy_file', '/dev/urandom');

// Uses a strong hash
ini_set('session.hash_function', 'whirlpool');

代码语言:javascript
复制
// **PREVENTING SESSION FIXATION**
// Session ID cannot be passed through URLs
ini_set('session.use_only_cookies', 1);

// Uses a secure connection (HTTPS) if possible
ini_set('session.cookie_secure', 1);

代码语言:javascript
复制
session_start();

// If the user is already logged
if (isset($_SESSION['uid'])) {
    // If the IP or the navigator doesn't match with the one stored in the session
    // there's probably a session hijacking going on

    if ($_SESSION['ip'] !== getIp() || $_SESSION['user_agent_id'] !== getUserAgentId()) {
        // Then it destroys the session
        session_unset();
        session_destroy();

        // Creates a new one
        session_regenerate_id(true); // Prevent's session fixation
        session_id(sha1(uniqid(microtime())); // Sets a random ID for the session
    }
} else {
    session_regenerate_id(true); // Prevent's session fixation
    session_id(sha1(uniqid(microtime())); // Sets a random ID for the session
    // Set the default values for the session
    setSessionDefaults();
    $_SESSION['ip'] = getIp(); // Saves the user's IP
    $_SESSION['user_agent_id'] = getUserAgentId(); // Saves the user's navigator
}

所以,我的问题是

  • ini_set是否提供了足够的安全性?
  • 是否可以保存用户的IP和导航器,然后在每次加载页面时进行检查以检测会话劫持?这是否会有任何问题?
  • 使用session_regenerate_id()是否正确?

<代码>H112使用<代码>D13是否正确?<代码>H214<代码>F215

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8419332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档