php - 需要帮助从数据库值创建会话
问题描述
(注意:已经查看了与此相关的类似问题,但在我的情况下这些问题对我不起作用)
所以下面的代码是用户使用他们的用户名和密码登录时的 php。该网站有一个在线货币系统。因此,当他们登录时,我想尝试获取该用户拥有的“金钱”数量,然后将其转换为 Session 变量,以便我可以在其他地方使用它来回显该值。
在此代码中,它根据登录框中的值创建一个会话。但我想根据他们当前的“令牌”值创建一个会话。
^ 因此,如果我尝试登录我的 acc“Nanikos”,我想从 token 列中获取我的 Tokens 的值,在本例中为 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 */
推荐阅读
- clojure - 如何使用 clojure 和 clojurescript 设置 SPA?
- python - 如何使用python将元素dict添加到数据框?
- css - How to override mui class?
- hadoop - Hive:由于最后一个 Reducer 作业,Inner Join 查询永远执行
- python - 为什么我的功能只在某些时候起作用?
- jquery - 以动态方式获取选定元素
- html - 如何在html中默认展开树视图
- javascript - 有没有办法从这个字符串中去除秒数?
- powershell - PowerShell - 按 ID 或位置(名称除外)引用列或属性
- ios - SwiftUI AppDelegate 无法正常工作