2014-02-28 178 views
0

我试图做一个简单的登录,该ID和密码的输入由用户与数据库中的数据PHP ID和密码验证

//getting the inputs 
$checkid = $_POST["id"]; 

$checkpassword = md5($_POST["pass"]); 

//getting the id and password of the id and password of the inputs 
$query = "SELECT id, password FROM login WHERE id=$checkid AND password=$checkpassword"; 

$res = mysqli_query($link, $query); 

$nres = mysqli_num_rows($res); 

//$nres should be 0 if the user inputs the right id but the wrong password 
//or viceversa, the only way that it $nres!=0 is that both inputs match the db, right? 
if ($nres == 0) { 
    header('Location: http://localhost:8888/login/login_fail.php'); 
    else 
    header('Location: http://localhost:8888/profile/profile.php'); 
    exit(); 

这是行不通的比较,甚至如果我把正确的ID和数据库上的密码重定向到login_fail.php。 注意:它工作,如果我只是用他的ID和取出查询“,密码”“和密码= $ checkpassword”。帮助

+2

添加引号'“SELECT ID,密码从登录WHERE ID = '$ checkid' 和密码='$ checkpassword ''''和一个旁注:不要使用'md5',现在用作密码存储是不安全的。 –

+1

+1,并且还要注意,由于不对POST变量进行任何处理,因此您可以广泛应对SQL注入攻击。 – JBES

+0

你也可以修正你的条件陈述的支撑。 'if {...} else {...}' –

回答

2

添加引号的变量:

"SELECT id, password FROM login WHERE id='$checkid' AND password='$checkpassword'" 
             ^ ^   ^   ^

旁注:不要使用md5,它现在不安全的密码存储使用。

对于密码存储,请使用bcrypt或PHP的password()函数。

而看到this article also

在被别人评论也指出,使用mysqli_real_escape_string()

$checkid=mysqli_real_escape_string($link,$_POST['id']); 
0

尝试查询:

$query = "SELECT id, password FROM login WHERE id='".$checkid."' AND password='".$checkpassword."'"; 
+0

你需要'因为你提交的数据库类型是char或varchar,也不是int或float。 –