我的目标是检查 cookie 中存储的“令牌”的值是否与数据库中的 loginTable 表中的值相同。我可以将 cookie 的值保存在变量 $token 中。我遇到的问题在这里 --> if ($row["token"] == $token)。例如$token的值为bm0h8sdigs,行中的值为bm0h8sdigs,但不进入if。我也尝试了 strcmp 函数,但它不起作用。if(strcmp($row["token"], $token) == 0) 我还尝试了 $sqlToken = "select * from loginTable where token='" 。$令牌。“'”;但它不起作用。
回显$令牌;打印我的 cookie 中的内容 - bm0h8sdigs
<!-- https://github.com/js-cookie/js-cookie This is a library to make handling cookies easier -->
<script>
var token = Cookies.get('token');
</script>
<?php
$token = "<script>document.write(token)</script>";
$var = 0;
include "connectDB.php";
$sqlToken = "SELECT * FROM loginTable";
$resultToken = $mysqli->query($sqlToken);
while($row = $resultToken->fetch_assoc()){
if ($row["token"] == $token){
$var = 1;
}
}
$mysqli->close();
if ($var == 1){
//Token found in db
}
else{
//Token not found in db
}
?>
连接数据库.php
<?php
$host = "localhost";
$username = "root";
$password = "usbw";
$database = "test";
$mysqli = new mysqli($host, $username, $password, $database);
$mysqli -> set_charset("utf8");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
?>
登录表 - prnt.sc/u7nidn
Create table loginTable(
loginID int AUTO_INCREMENT,
userID int not null,
token varchar(20) not null,
Constraint PK_loginTable Primary Key(loginID),
Constraint FK_loginTable_userID Foreign Key (userID) References usersTable (userID)
)
白猪掌柜的