有人可以帮我吗?我的用户名是Blimeo,我的密码是"密码",但是当我输入凭据时,它像我告诉它的那样显示"访问被拒绝"。我 100% 确定我正确配置了我的 mySQL 数据库。
<html>
<body>
<?php
echo sha1('Blimeo');
if (isset($_REQUEST['attempt'])) {
$link = mysql_connect('localhost', 'root', 'password') or die('Could not connect to database');
$user = mysql_real_escape_string($_POST['user']);
$password = sha1(mysql_real_escape_string($_POST['password']));
mysql_select_db('test_users');
$query = mysql_query(
"SELECT user
FROM users
WHERE user = '$user'
AND password = '$password'
") or die(mysql_error());
mysql_fetch_array($query);
$total = mysql_num_rows($query);
if ($total > 0) {
session_start();
$_SESSION['user'] = 'blah';
header('location: dashboard.php');
}
else {
echo '<br>Access denied!';
}
}
?>
<form method="post" action="login.php?attempt">
Enter your username:<input type="text" name="user"/><br/>
Enter your password:<input type="password" name="password"/><br/>
<input type="submit"/>
</form>
</body>
</html>
更新,2016
年请只使用现有的登录系统,这些系统在几乎每个PHP框架中都是开箱即用的!绝对没有理由自己写这个,因为用户身份验证是一个大话题,编写一个严肃、稳定和现代的登录解决方案需要数月(数年)。
原文,2012年:
由于登录系统是一个安全问题,每个人都会一遍又一遍地犯同样的错误,我可以清楚地说:
使用专业脚本并完成代码以了解发生了什么,什么是哈希和加盐以及会话可能存在的问题。
[删除过时链接]
以下是您可能需要的三个项目:
https://github.com/panique/php-login-one-file
https://github.com/panique/php-login-minimal
https://github.com/panique/huge
首先,您应该在页面的第一行开始会话。
您还应该更新代码以使用 PDO 语句而不是 mysql 函数。这些速度较慢且容易出现 mysqli。
然后,您需要检查返回的行数是否等于 1 且不大于 0。这将是一个安全问题,因为可以操纵您的脚本返回超过 1 行,然后它会验证并进入安全区域。
在我看来,问题在于,您的密码与数据库不匹配。 回显密码的sha1,看看它是否与表匹配。
似乎脚本中断的地方是在测试 mysql_num_rows() 时。
就在之前:
if ($total > 0)
{
也许尝试添加以下行进行测试并确保$total确实> 0:
echo $total;
除此之外,请尝试测试 mysql 查询以确保它至少从数据库返回 1 行。
make database in mysql then run this code::
<table border="0" align="center" cellpadding="0" cellspacing="0" width="300">
<tr>
<td>
<form method="post" action="flogin.php">
<table width="100%" cellpadding="7" cellspacing="0" border="0">
<tr>
<td colspan="3"><center><strong>Insert Values In DataBase </strong></center><br /></td><br />
</tr>
<tr>
<td width="30%">Name</td>
<td width="10%">:</td>
<td width="60%"><input type="text" name="name" /></td>
</tr>
<tr>
<td width="30%">Last Name</td>
<td width="10%">:</td>
<td width="60%"><input type="text" name="lastname" /></td>
</tr>
<tr>
<td width="30%">Email</td>
<td width="10%">:</td>
<td width="60%"><input type="text" name="email" /></td>
</tr>
<tr>
<td colspan="3"><center><input type="submit" name="submit" /></center><br /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php
mysql_connect("localhost", "root", "") or die("can not connect to database");
mysql_select_db("flogin")or die("can not connect");
if (isset($_POST['submit'])){
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$query=mysql_query("INSERT INTO info(name, lastname, email)VALUES('$name', '$lastname', '$email')");
if($query){
echo "successful";
echo "<br>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {
echo "error";
}
}
?>
<?php
mysql_close();
?>