gpt4 book ai didi

PHP 登录脚本无法正常工作

转载 作者:行者123 更新时间:2023-11-29 08:22:56 25 4
gpt4 key购买 nike

我正在尝试使用 php 和 mysql 制作登录表单,但过去 3 天失败。

我基本上有 php 表单,并且在同一页面上有用于将用户登录到网站的脚本。

表单.php

<form id="superAdminForm" method="post" action="">
<input type="email" name="email" required class="txtInput" placeholder="Email..." autocomplete="off"/> <br />
<input type="password" name="password" required class="txtInput" placeholder="Password..."/> <br />
<input type="submit" name="submit" value="Enter" id="submit" />
</form>

form.php 的 php 代码

<?php
require("../php_includes/db-connect.php");

if (isset($_POST["submit"])) {
$email = $_POST["email"];
$password = $_POST["password"];

$sql = "SELECT * FROM users WHERE email='$email' AND password='$password' LIMIT 1";
$query = mysqli_query($con, $sql);

$row = mysqli_fetch_array($query);

if ($row['email'] == 1) {
header("Location: admin-index.php");
}
}

mysqli_close($con);

?>

问题是登录没有成功。我不知道其背后的原因。所以请在这方面帮助我。还请指导我犯错误的地方。

我是 PHP 新手,正在尽力掌握我的知识。

最佳答案

1..谈论安全

您的代码容易受到 sql injection 的攻击所以即使它能工作也没有用

enter image description here

^^ source

' 或 '1'='1'/* ' 会产生魔法

所以要么使用 mysqli_real_escape_string函数 用于手动转义字符串中的特殊字符或使用 prepared statements和参数化查询(推荐)。

2.. email='$email' AND 密码='$password' LIMIT 1";

那么为什么电子邮件不应该是唯一的或者在登录时检查电子邮件是否已经存在如果存在则显示错误消息,例如(电子邮件已存在)

所以你不需要使用limit 1,这确实没有意义

3..为什么不在SESSION中设置登录ID或其他内容,以便确定用户是否已登录

您的上述登录脚本不执行任何操作,它只是检查电子邮件和密码是否存在于数据库中

4.. 计划文本作为密码确实是个坏主意,而是使用哈希检查此 How do you use bcrypt for hashing passwords in PHP?

好书

  1. How can I prevent SQL injection in PHP?

关于PHP 登录脚本无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18854196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com