我已经成功创建了一个注册表单,它工作正常。但是我的登录系统显示错误,我无法弄清楚。每当我使用登录时,即使我使用完全相同的用户和密码,它也会在 URL 中显示 login==empty。
<?php
session_start();
include 'conn.php';
if (isset($_POST['submit'])) {
$uid=mysqli_real_escape_string($conn,'$_POST["firstname"]');
$pwd=mysqli_real_escape_string($conn,'$_POST["password"]');
if ($uid==""||$pwd=="") {
header("Location:../triallogin.php?login=empty");
exit();
} else{
$sql="SELECT * FROM comments WHERE first='$uid'";
$result=mysqli_query($conn,$sql);
$resultcheck=mysqli_num_rows($result);
if ($resultcheck<1) {
header("Location:../triallogin.php?login=empty");
exit();
} else{
if ($row=mysqli_fetch_assoc($result)) {
//De-hasing of password
$hashedpassowrd=password_verify($pwd,$row['pwd']);
if ($hashedpassowrd==false) {
header("Location:../triallogin?login=wrongpassword");
} elseif ($hashedpassowrd==true) {
$_SESSION['first']=$row['first'];
$_SESSION['last']=$row['last'];
header('Location:../triallogin.php?login=success');
exit();
}
}
}
}
} else {
header('Location:../triallogin.php?login=hit');
exit();
}
最佳答案
三件事。
您已将帖子变量作为字符串:
$uid=mysqli_real_escape_string($conn,'$_POST["firstname"]');
$pwd=mysqli_real_escape_string($conn,'$_POST["password"]');
将其更改为:
$uid = mysqli_real_escape_string($conn, $_POST["firstname"]);
$pwd = mysqli_real_escape_string($conn, $_POST["password"]);
其次,您试图从
comments
获取您的用户登录详细信息。 table ?这不可能。$sql="SELECT * FROM comments WHERE first='$uid'";
可能将其更改为
users
表,或任何你称之为的东西。最后,您的 SQL 容易受到 SQL 注入(inject)攻击。解决方法是使用准备好的语句并绑定(bind)参数。
我建议在 mysqli 上使用 PDO,这可能是解释原因的最佳文章之一,请阅读! https://phpdelusions.net/pdo
关于php - 如何解决我的登录问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50738073/