Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将MySQL中的id添加到会话cookie_Php_Mysql_Session_Cookies - Fatal编程技术网

Php 如何将MySQL中的id添加到会话cookie

Php 如何将MySQL中的id添加到会话cookie,php,mysql,session,cookies,Php,Mysql,Session,Cookies,我按照教程在PHP和MySQL中创建了一个安全的登录脚本 在那里,我们创建了一个函数,该函数创建了一个安全会话,并用它创建了一个cookie <?php function sec_session_start() { $session_name = 'COOKIENAME'; // Set a custom session name $secure = SECURE; // This stops JavaScript being able to acces

我按照教程在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. 
}
?>

链接到教程

您可以添加这样的会话$\u会话['user\u id']=$user\u id;
会话只是一个不会消失的数组,您可以通过$\u session[]访问它。

您的想法是正确的。阅读说明:您永远无法“更新”cookie,您只能使用
setcookie()
功能覆盖cookie,这不是在
$\u会话['user\u id']=$user\u id中完成的吗?执行
var\u转储($\u会话)您将看到您的用户id已全部就绪,存储在:pi get“'login_string'=>string'9BF140A86B620CBA0C2FBCC7866AD71C84C99B68D2FDB及其他”中,但如果我回显$_COOKIE[“COOKIENAME”],则它不是数据库中的id号;我的感觉一直在变化,就像这样:11tnlfartliggm5glaehkff0eu2i得到了我需要使用的东西,我很高兴我只花了几个小时就找到了方法。谢谢
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;
    }
}