【问题标题】:Need help creating a session from database values需要帮助从数据库值创建会话
【发布时间】:2018-07-17 00:31:59
【问题描述】:

(注意:已经查看了与此相关的类似问题,但在我的情况下这些问题对我不起作用)

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

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

(Image of Phpmyadmin database)

^ 因此,如果我尝试登录我的 acc“Nanikos”,我想从令牌列中获取令牌的值,在本例中为 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();
}

【问题讨论】:

  • $_SESSION["UID"] 不工作吗?出了什么问题?
  • 您应该使用准备好的语句并丢弃那些转义字符串。除此之外,您可能希望显示您在哪里使用会话数据。你也包括 session_start() 吗?
  • 在我看来,用户的代币余额不应该存储在会话中。无论哪种方式,您都需要在添加/删除令牌时更新数据库,因此您最好只使用数据库。

标签: php variables session


【解决方案1】:

在这个页面,修改你登录用户的部分:

        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 */

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 2022-07-04
    • 2015-04-26
    • 2010-11-08
    • 2020-02-16
    • 1970-01-01
    相关资源
    最近更新 更多