gpt4 book ai didi

php - 在 PHP/mysql 中使用 SHA1 密码加密的问题

转载 作者:行者123 更新时间:2023-11-29 12:12:24 29 4
gpt4 key购买 nike

我有一个用于用户登录的简单脚本。我在 mySQL 数据库表中将密码加密为 SHA1(使用字符集 utf8_unicode_ci)。

当我在数据库中使用值运行“$q”时,它会正常返回结果。但即使在输入正确的凭据后,通过脚本我也无法登录。另外,如果我删除两个地方(脚本和数据库)的加密,它就可以正常工作。如果我使用 MD5 代替,也会出现同样的问题。

我不确定我错过了什么。我尝试回显 SHA1 输出,结果与数据库中可见的加密密码不同。我还检查了输入中是否有多余的空格。请帮助我理解出了什么问题。需要帮助请叫我。提前致谢!

connection.php 保存数据库的登录凭据和以下行:

$dbc = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());

下面是登录页面:“login.php”

<?php 

#Start the session:
session_start();
include('../setup/connection.php');

if($_POST) {
$q = "select * from users where email = '$_POST[email]' and password = SHA1('$_POST[password]');";
$r = mysqli_query($dbc, $q);

if (mysqli_num_rows($r) == 1) {
$_SESSION['username'] = $_POST['email'];
header('Location: index.php');
}
else {$msg="Username/Password incorrect. Please try again!";}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="utf-8">
<title>Admin Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php include('config/css.php'); ?>
<?php include('config/favicon.php'); ?>
<?php include('config/js.php'); ?>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/r29/html5.min.js"></script>
<![endif]-->

</head>

<body>

<!--NAVIGATION BAR-->
<?php //include(D_TEMPLATE.'/navigation.php'); ?>
<div class="container">


<div class="col-lg-4 col-lg-offset-4">
<div class="panel panel-info">
<div class="panel-heading">
<h1 class="lato fs20"><strong>Login</strong></h1>
</div>
<div class="panel-body">
<?php echo $msg; ?>
<form role="form" method="post" action="login.php">
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" class="form-control" name="password">
</div>

<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
</div>
</div>
</body>

</html>

最佳答案

对于“$q”变量,您应该使用 php sha1 函数:

 $q = "select * from users where email = '$_POST[email]' and password = '" . sha1($_POST[password]) . "'";

但正如 Fred-ii 所说,你之前确实应该(必须)保护你的变量。例如:

$_POST['email'] = mysqli_real_escape_string($_POST['email']);

它将保护您的变量免受 SQL 注入(inject) ( https://php.net/manual/en/mysqli.real-escape-string.php )

关于php - 在 PHP/mysql 中使用 SHA1 密码加密的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30413951/

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