(注意:已经研究了与此类似的问题,但对我而言这些问题对我没有帮助)
因此,以下代码是用户使用其用户名和密码登录时的php。该网站有一个在线货币系统。因此,当他们登录时,我想尝试获取该用户拥有的“钱”,然后将其转换为Session变量,以便在其他地方使用它来回显该值。
在此代码中,它将基于登录框中的值创建一个会话。但是我想根据其当前的“令牌”值创建一个会话。
(Image of Phpmyadmin database)
^因此,如果我尝试登录我的帐户“ Nanikos”,我想从令牌列中获取令牌的值,在这种情况下为1000。然后将其转换为会话,以便我可以回显网站周围有1000个。
因为我从未在aha之前尝试过这样做,所以不确定该怎么做。
<?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();
}
答案 0 :(得分:0)
在此页面上,修改您登录用户的部分:
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 */