2013-03-02 37 views
0

我是mysql和php的新手。用户使用php和mysql数据库登录

一直在为用户创建一个带有表的数据库。

我已经成功地将用户添加到数据库,他们的密码与md5(是的,我知道它不安全),它不会在线启动。

我的问题是,如何根据用户名和密码登录用户。

这里是我的代码

我的逻辑是taht查询运行后,它会返回true或false。

如果为true,则显示登录成功,否则不成功。

然而,即使我输入正确的用户名和密码,我仍然得到一个不成功的登录信息

我检查了MySQL数据库,以及uesrname是有正确

想法?

if(!empty($_POST['userLog']) && !empty($_POST['passLog'])) 
{ 
    //set the username and password variables from the form 
    $username = $_POST['userLog']; 
    $password = $_POST['passLog']; 

    //create sql string to retrieve the string from the database table "users" 
    $sql = "SELECT * FROM `users` WHERE userName = '$username' AND password = md5('$password')"; 
    $result = mysql_query($sql); 
     if ($result == true) { 
      $return = "<font color=#008000><Center><b>**Successful Login**</b></Center></font>"; 
     } else { 
      $return = "<font color=#ff0000><Center><b>**Failed Login**</b></Center></font>"; 
     } 
     print($return); 
} 
+1

看起来像md5(密码)将成为qry字符串的一部分。尝试将其更改为'$ sql =“SELECT * FROM \'users \'WHERE userName ='$ username'AND password ='”.md5('$ password')。“'”;' – SchautDollar 2013-03-02 21:38:39

+0

快速提示使用phpass - > http://www.openwall.com/phpass/ – Espen 2013-03-02 21:43:18

回答

1

我不完全确定你的SQL会运行,但只是为了安全起见。

更改它,以便

$password_hash = md5($password); 

$sql = "SELECT * FROM `users` WHERE userName = '$username' AND password = '$password_hash'"; 

而对于你原来的问题

if(mysql_num_rows($result) == 1) { //If the SQL returns one row, that means that a user was found with `userName = $username` and `password = md5($password)` 
    // Login 
} else { 
    // Authentication Failed 
} 

而且,因为它现在已经贬值考虑使用库MySQLi而不是MySQL的。

+0

我试过这个解决方案,并且输出了进入sql qauery的值,并且它们是正确的。然而,我仍然得到一个失败的登录,并且我得到一个错误mysql_num_rows()期望参数1是资源,布尔值 – user1050632 2013-03-02 21:58:05

+0

您是否更新了上面提到的$ sql变量? – nine7ySix 2013-03-02 22:01:09

+0

是的,我做了,并且我打印了新的passwordHash值,它匹配了mysql表中的内容 – user1050632 2013-03-02 22:02:54

0

首先,保护您的代码免受SQL injections的侵害。

然后,确保数据库中的密码真的用md5()函数散列。 确保您的表单使用POST方法将数据传递给脚本。

试试下面的代码:

if(!empty($_POST['userLog']) && !empty($_POST['passLog'])) 
{ 
    //set the username and password variables from the form 
    $username = $_POST['userLog']; 
    $password = $_POST['passLog']; 

    //create sql string to retrieve the string from the database table "users" 
    $sql = "SELECT * FROM `users` WHERE userName = '". addslashes($username) ."' AND password = '". md5('$password')."'"; 
    $result = mysql_query($sql); 
     if (mysql_num_rows($result)>0) { 
      $return = "<font color=#008000><Center><b>**Successful Login**</b></Center></font>"; 
     } else { 
      $return = "<font color=#ff0000><Center><b>**Failed Login**</b></Center></font>"; 
     } 
     print($return); 
} 
0

mysql_query没有返回TRUE或FALSE。根据文档(http://php.net/manual/en/function.mysql-query.php),如果成功则返回资源;如果有错误,则返回FALSE。您需要评估资源以查看它是否有效。

if(!empty($_POST['userLog']) && !empty($_POST['passLog'])) 
{ 
    //set the username and password variables from the form 
    $username = $_POST['userLog']; 
    $password = $_POST['passLog']; 

    //create sql string to retrieve the string from the database table "users" 
    $sql = "SELECT * FROM `users` WHERE userName = '$username' AND password = md5('$password')"; 
    $result = mysql_query($sql); 
    if ($result && $row = mysql_fetch_assoc($result)) { 
     $return = "<font color=#008000><Center><b>**Successful Login**</b></Center></font>"; 
    } else { 
     $return = "<font color=#ff0000><Center><b>**Failed Login**</b></Center></font>"; 
    } 
    print($return); 
} 
0

正如我的评论中提到的,这个问题似乎是你的sql字符串。不是散列,而是将该方法放入字符串中。因此,改变

$sql = "SELECT * FROM `users` WHERE userName = '$username' AND password = md5('$password')"; 

$sql = "SELECT * FROM `users` WHERE userName ='$username' AND password = '".md5('$password')."'"; 

您的结果不会是真的还是假的,但由于PHP对待任何值不是0为真,这将作为是。 此外,强烈建议将所有数据转入您的sql字符串以防止sql注入。另外需要注意的是:mysql已经被弃用了,所以现在将是移植到mysqli之类的好时机。