php - 如何解决我的登录问题?
问题描述
我已经成功创建了一个注册表单,它工作正常。但是我的登录系统显示错误,我无法弄清楚。每当我使用登录时,即使我使用完全相同的用户和密码,它也会在 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
表中获取您的用户登录详细信息?这不可能。
$sql="SELECT * FROM comments WHERE first='$uid'";
可能将其更改为users
表格,或任何您称之为的内容。
最后,您的 SQL 容易受到 SQL 注入攻击。解决方法是使用准备好的语句并绑定参数。
我建议在 mysqli 上使用 PDO,这可能是解释原因的最佳文章之一,请阅读!https://phpdelusions.net/pdo
推荐阅读
- java - 我应该如何在 Spring 中返回响应实体
- fabricjs - Fabric JS在图像容器内移动图像
- vega - 如何从 Vega 回归变换中获取系数?
- macos - 可以在 Visual Studio for Mac 中显示光标行和列吗?
- c# - 我应该在 Xamarin.Forms 上使用哪些 SQLite (sqlite-net-pcl) 标志?
- python - 凯撒密码 Python 查询
- cmake - 使用 Clang 使用 Bazel 构建 OpenCV
- c# - ERR_HTTP2_INADEQUATE_TRANSPORT_SECURITY 使用 HTTPS 在 localhost 上运行 .net5.0 隔离功能时
- visual-studio-code - 如何清除 Plugin svelte: A11y: on:blur error/warning?
- windows - How can I rename file extensions to another extension type, on another directory?