我目前正在使用PHP创建一个简单的登录系统,并且在写这个
时$sql = "SELECT * FROM logintoken WHERE token ='".$_COOKIE['SNID']."';";
它没有给出任何结果,尽管当我回应$ _COOKIE [' SNID']时,它吐出了正确的结果。我还检查了数据库,其中的值是'令牌'我怎么能解决这个问题呢。感谢您阅读本文。这是我的代码:
<?php
if(isset($_COOKIE['SNID'])){
echo 'Logged in';
echo '<form action="logout.weg.php" method="post">
<button type="submit" name="logout">Press to logout</button>
</form>';
$userid = $_COOKIE['SNID'];
$sql = "SELECT * FROM logintoken WHERE token ='".$_COOKIE['SNID']."';";
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result) > 0){
while ($row= mysqli_fetch_assoc($result)){
echo $row['user_id'];
}
} else {
echo 'No result';
}
} else {
echo 'Not logged in';
}
?>
答案 0 :(得分:0)
您应该使用mysqli_real_escape_string()
完全转义Cookie值。
问题也可能是您的$result = mysqli_query($conn,$sql) or error_log(print_r(mysqli_error($conn),true));
。从SQL中删除它。 PHP Manaul说:
注意:强>
您不应该在语句中添加终止分号或\ g。
此外,您应该检查您的MySQL和PHP error logs并使用错误输出,例如:
/***
* Never trust user data. Including cookies.
* Here assume cookie random token key is any alphanumeric character.
* This key is NOT the password
***/
$userid = $_COOKIE['SNID'];
$useridClean = preg_replace("/[^a-z0-9]/i","",$userid); // clean the cookie value
if($useridClean !== $userid){
die("bad cookie!");
}
/***
* Hash your cookie value
***/
$useridClean = hash('sha256', $userid);
/***
* NOTE: Do NOT append ; at the end of the statement.
* Good practise to check the user is expecting to be "remembered"
***/
$sql = "SELECT username, userid FROM logintoken WHERE token = ? AND
remember_me_flag = 'Y' AND user_banned_flag != 'Y' ";
/***
* Use prepared statements for safety
***/
$mysqli = new mysqli("example.com", "user", "password", "database");
$result = $mysqli->prepare($sql);
$result->bind_param("s", $useridClean);
$result->execute();
$row = $result->fetch_array(MYSQLI_ASSOC);
$result->close();
if($row['userid'] > 0 && !empty($row['username'])){
/***
* Once the user has returned; reasign new token values in both
* The database and the cookie
***/
echo "Hello ".$row['username'];
}
}
else {
echo 'No result';
}
(资格赛:这不是最好的方式,但这确实可以快速完成工作)
(资格赛:您尝试做的可能不是最好的方式,但是这里
def put_splitted_text_in_blackboard(blackboard, splitted_text):
draw = ImageDraw.Draw(blackboard)
for text in splitted_text:
fonts = ImageFont.truetype("preeti.TTF", 50)
draw.text((10, 25), text, font=fonts)
答案 1 :(得分:-1)
使用$ _SESSION。切勿使用cookie来保存密码或登录信息,因为它很容易被黑客攻击! $ _SESSION使用特定ID保存cookie,但信息存储在PHP服务器中。
尝试将Cookie打印为数组,使用&#34; print_r&#34;功能。还要仔细检查cookie是否设置了相同的名称,cookie名称是CASE SENSETIVE!