我发布了我的问题here,在我编辑帖子之前,它已被关闭,因为不是真正的问题!
我有一个这样的登录表单:
<html>
<head>
<title>Password Checking Script</title>
</head>
<body>
<form action="check_user-pass.php" method="POST">
<h3>Please Login</h3>
User Name: <input type="text" name="user_name"><br>
Password: <input type="password" name="password">
<input type="submit" name="submit" value="Login!">
</form>
</body>
</html>
如您所见,此表单通过check_user-pass.php
对用户进行身份验证
它在我的数据库中查找这些凭据;如果它们存在,则返回OK
,否则返回值NO
。
所以我的问题是:我应该在check_user-pass.php
中准确包含哪些代码?
我试图添加更多代码,但也不能这样做!我目前的代码是:
<?php
ob_start();
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("$db_name") or die(mysql_error());
echo "Connected to Database<br />";
// Check $username and $password
/*
if(empty($_POST['username']))
{
echo "UserName/Password is empty!";
return false;
}
if(empty($_POST['password']))
{
echo "Password is empty!";
return false;
}
*/
// Define $username and $password
$username=$_POST['username'];
$password=md5($_POST['pass']);
// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if ($count==1) {
echo "Success! $count";
} else {
echo "Unsuccessful! $count";
}
ob_end_flush();
?>
答案 0 :(得分:3)
您表单中的名称为user_name
,但在您的脚本中,您会查找username
$username=$_POST['username'];
应该是
$username=$_POST['user_name'];
编辑:
如果在将密码放入数据库之前使用crypt加密密码,请尝试使用
$sql="SELECT * FROM $tbl_name WHERE username='$username'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1){
$row = mysql_fetch_assoc($result);
if (crypt($password, $row['password']) == $row['password']){
session_register("username");
session_register("password");
echo "Login Successful";
return true;
}
else {
echo "Wrong Username or Password";
return false;
}
}
else{
echo "Wrong Username or Password";
return false;
}
编辑: myBB似乎使用了大量的md5哈希密码,试试这个
$sql="SELECT * FROM $tbl_name WHERE username='$username'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1){
$row = mysql_fetch_assoc($result);
if (md5(md5($row['salt']).md5($password)) == $row['password']){
session_register("username");
session_register("password");
echo "Login Successful";
return true;
}
else {
echo "Wrong Username or Password";
return false;
}
}
else{
echo "Wrong Username or Password";
return false;
}
同样哈希是一种方法,因此您无法取回数据库中已有的密码,您只需要让用户更改/更新其密码。
如果以上工作,那么你就不必关闭加密,一切都应该没问题。
答案 1 :(得分:2)
大多数情况下,mysql数据库中的密码都是加密的。如果您直接将它们输入数据库,它们可能没有盐。在将密码传递给查询之前,请尝试加密密码:
$crypted_pass = crypt($password);
如果有一个盐(通常是用户名的前两个字母),请将其传递给crypt函数:
$salt = substr($username, 0, 2);
$crypted_pass = crypt($password, $salt);
答案 2 :(得分:2)
尝试简单的事情:
if ($count==1) {
echo "Success! $count";
} else {
echo "Unsuccessful! $count";
}
这将显示您是否返回任何大于1或更多值的内容。