【问题标题】:How to add an id from MySQL to a session cookie如何将 MySQL 中的 id 添加到会话 cookie
【发布时间】:2016-05-07 04:22:59
【问题描述】:

我按照教程在 PHP 和 MySQL 中创建安全登录脚本 在那里,我们创建了一个创建安全会话并使用它创建 cookie 的函数。

<?php
    function sec_session_start() {
    $session_name = 'COOKIENAME';   // Set a custom session name
    $secure = SECURE;
    // This stops JavaScript being able to access the session id.
    $httponly = true;
    // Forces sessions to only use cookies.
    if (ini_set('session.use_only_cookies', 1) === FALSE) {
        header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
        exit();
    }
    // Gets current cookies params.
    $cookieParams = session_get_cookie_params();
    session_set_cookie_params($cookieParams["lifetime"],
        $cookieParams["path"], 
        $cookieParams["domain"], 
        $secure,
        $httponly);
    // Sets the session name to the one set above.
    session_name($session_name);
    session_start();            // Start the PHP session 
    session_regenerate_id();    // regenerated the session, delete the old one. 
}
?>

现在我想将我在 mysql 数据库中的用户 ID 添加到该 cookie 我想稍后使用这些 ID,以便在需要时从用户那里获取更多数据。 或存储他们选择的设置。

我该怎么做? 我应该在登录功能中添加一些东西,以便在登录时将 ID 添加到会话 cookie 中吗?

function login($email, $password, $mysqli) {
// Using prepared statements means that SQL injection is not possible. 
if ($stmt = $mysqli->prepare("SELECT id, username, password, salt 
    FROM members
   WHERE email = ?
    LIMIT 1")) {
    $stmt->bind_param('s', $email);  // Bind "$email" to parameter.
    $stmt->execute();    // Execute the prepared query.
    $stmt->store_result();

    // get variables from result.
    $stmt->bind_result($user_id, $username, $db_password, $salt);
    $stmt->fetch();

    // hash the pasword with the unique salt.
    $password = hash('sha512', $password . $salt);
    if ($stmt->num_rows == 1) {
        // If the user exists we check if the account is locked
        // from too many login attempts 

        if (checkbrute($user_id, $mysqli) == true) {
            // Account is locked 
            // Send an email to user saying their account is locked
            return false;
        } else {
            // Chec k if the password in the database matches
            // the password the user submitted.
            if ($db_password == $password) {
                // Password is correct!
                // Get the user-agent string of the user.
                $user_browser = $_SERVER['HTTP_USER_AGENT'];
                // XSS protection as we might print this value
                $user_id = preg_replace("/[^0-9]+/", "", $user_id);
                $_SESSION['user_id'] = $user_id;
                // XSS protection as we might print this value
                $username = preg_replace("/[^a-zA-Z0-9_\-]+/", 
                                                            "", 
                                                            $username);
                $_SESSION['username'] = $username;
                $_SESSION['login_string'] = hash('sha512', 
                          $password . $user_browser);
                // Login successful.
                return true;
            } else {
                // Password is not correct
                // We record this attempt in the database
                $now = time();
                $mysqli->query("INSERT INTO login_attempts(user_id, time)
                                VALUES ('$user_id', '$now')");
                return false;
            }
        }
    } else {
        // No user exists.
        return false;
    }
}

教程链接http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL

【问题讨论】:

  • 你的想法是对的。阅读php.net/manual/en/function.setcookie.php 请注意,您永远无法“更新”cookie,只能使用setcookie() 函数覆盖它
  • $_SESSION['user_id'] = $user_id; 不是已经完成了吗?做var_dump($_SESSION);你会看到你的用户ID已经存储在:p
  • 我得到 "'login_string' => string '9bf140a86b620cba0c2fabcc7866ad71c84c99b68d2fdb and some more" 但它不是数据库中的 ID 号
  • 如果我回显 $_COOKIE["COOKIENAME"];我得到了一个一直在变化的刺痛,像这样:11tnlfartliggm5glaehkff0eu2
  • 我知道了我需要使用 我很高兴我只花了几个小时就找到了如何做到这一点。谢谢!

标签: php mysql session cookies


【解决方案1】:

您像这样添加会话 $_SESSION['user_id'] = $user_id; 会话只是一个不会消失的数组,您可以通过 $_SESSION[]

访问它

【讨论】:

    猜你喜欢
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 2015-02-23
    • 2011-04-05
    • 2023-04-01
    • 2013-04-28
    • 2013-07-19
    相关资源
    最近更新 更多