Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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 需要帮助从数据库值创建会话吗_Php_Variables_Session - Fatal编程技术网

Php 需要帮助从数据库值创建会话吗

Php 需要帮助从数据库值创建会话吗,php,variables,session,Php,Variables,Session,(注意:我已经研究过与此相关的类似问题,但这些问题在我的案例中并不适用) 下面的代码是用户使用用户名和密码登录时的php代码。该网站有一个在线货币系统。因此,当他们登录时,我想尝试获取该用户拥有的“钱”的数量,然后将其转换为会话变量,以便我可以在其他地方使用它来响应该值 在此代码中,它根据登录框中的值创建会话。但我想用它们当前的“令牌”值创建一个会话 () ^因此,如果我试图登录我的acc“Nanikos”,我想从token列中获取我的token值,在本例中为1000。然后把它变成一个会话,这样

(注意:我已经研究过与此相关的类似问题,但这些问题在我的案例中并不适用)

下面的代码是用户使用用户名和密码登录时的php代码。该网站有一个在线货币系统。因此,当他们登录时,我想尝试获取该用户拥有的“钱”的数量,然后将其转换为会话变量,以便我可以在其他地方使用它来响应该值

在此代码中,它根据登录框中的值创建会话。但我想用它们当前的“令牌”值创建一个会话

()

^因此,如果我试图登录我的acc“Nanikos”,我想从token列中获取我的token值,在本例中为1000。然后把它变成一个会话,这样我就可以在网站上回传这1000条信息

只是不知道该怎么做,因为我以前从来没有试过这么做啊哈

<?php

session_start();

$servername = "localhost";
$username = "czt_Nanikos";
$password = "CZTCb030499";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Select Database
mysqli_select_db($conn, "czt_database");

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$uid = mysqli_real_escape_string($conn, $_POST['uid']);
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);

//Error handlers
    if (empty($uid) || empty($pwd)) {
        header("Location: ../index.php?login=empty");
    } else {    
        $sql = "SELECT * FROM users WHERE user_uid='$uid'";
        $result = mysqli_query($conn, $sql);
        $resultCheck = mysqli_num_rows($result);
        if ($resultCheck < 1) {
            header("Location: ../index.php?login=error");
            exit();
        } else {
            if ($row = mysqli_fetch_assoc($result)) {
                //De-hashing the password
                $hashedPwdCheck = password_verify($pwd, $row['user_pwd']);
                if ($hashedPwdCheck == false) {
                    header("Location: ../index.php?login=error");
                    exit();
                } elseif ($hashedPwdCheck == true) {
                    //Log in the user
                    $_SESSION["UID"] = $uid;
                    header("Location: ../index.php");
                    exit();
                }
            }
        }
    }
{
    header("Location: ../index.php?login=error");
    exit();
}

在此页面上,修改您登录用户的部分:

        if ($row = mysqli_fetch_assoc($result)) {
            //De-hashing the password
            $hashedPwdCheck = password_verify($pwd, $row['user_pwd']);
            if ($hashedPwdCheck == false) {
                header("Location: ../index.php?login=error");
                exit();
            } elseif ($hashedPwdCheck == true) {
                //Log in the user
                $_SESSION["UID"] = $uid;

                /**
                 * Add the line below ...............::
                 **/
                $_SESSION['token'] = $row['tokens'];

                header("Location: ../index.php");
                exit();
            }
        }
在下一页中,请执行以下操作:

<?php
    session_start();
    if (array_key_exists('token', $_SESSION) {
        $token = $_SESSION['token'];
    } else {
        //redirect to login page
    }

    /* continue code for page */

会话[“UID”]
是否不工作?问题是什么?你应该使用预先准备好的语句并扔掉那些转义字符串。除此之外,您可能还需要显示会话数据的使用位置。您是否也包括session_start()?在我看来,用户的令牌余额不应该存储在会话中。无论哪种方式,在添加/删除令牌时都需要更新数据库,因此您最好只使用数据库。