dongqiao8421 2012-07-19 14:32
浏览 81
已采纳

数据库中的{plain}密码和登录错误

I am coding a login panel to access to an Administration Panel. The data (username and passw) are stored in a MySQL Database (type: InnoDB). Looking in the tables the passwords are stored as plain and in the field password I have: {plain}password.

Adapting a code that I already have, I have some problems because that {plain} thing is confusing me a bit.

My old code is:

// Construct SQL statement for query & execute 
$sql = "SELECT * FROM table WHERE user = '" . $username . "' AND password = '" . md5($password) . "'"; 
$result = $mysqli->query($sql) or die(mysql_error());

So if I replace the "{plain}password" from the field in the database with an MD5 password, the code works great, but if I modify my code to the following one:

// Construct SQL statement for query & execute 
$sql = "SELECT * FROM table WHERE user = '" . $username . "' AND password = '" . $password . "'"; 
$result = $mysqli->query($sql) or die(mysql_error());

I can't login because the password is wrong!

Any idea how to fix this?

  • 写回答

2条回答 默认 最新

  • duanfen9090 2012-07-19 14:36
    关注

    If there is the prefix "{plain}" in front of the real password, you have to adjust your query to include that prefix.

    $sql = "SELECT * FROM table WHERE user = '" . $username . "' AND password = '{plain}" . $password . "'"; 
    $result = $mysqli->query($sql) or die($mysqli->error());
    

    Also note that you should change mysql_error() in the die() command to use mysqli functions as well (so use $mysqli->error).

    PS: You should have a look at how to store password nowadays. Storing them in plain text is not secure by any means.

    EDIT

    Mentioning the comment by @BrianRasmussen here as well: Make sure $username and $password have been sanitzed before being used directly (using string concat) in a query! Otherwise your code is open to SQL injections of all sorts.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)