dongliangkeng1056 2011-01-10 01:46
浏览 35

如何使用md5哈希?

Okay, so I'm learning php, html, and mysql to learn website development (for fun). One thing I still don't get is how to use md5 or sha1 hashes. I know how to hash the plain text, but say I want to make a login page. Since the password is hashed and can't be reversed, how would mysql know that the user-inserted password matches the hashed password in the database? Here is what I mean:

$password = md5($_POST['password']);
$query = ("INSERT INTO `users`.`data` (`password`) VALUES ('$password')");

I know that this snippet of script hashes the password, but how would I use this piece of code and make a login page? Any working examples would be great.

Here is my script:

<?php  

session_start();  

include("mainmenu.php");  

$usrname = mysql_real_escape_string($_POST['usrname']); 
$password = md5($_POST['password']);  

$con = mysql_connect("localhost", "root", "g00dfor@boy"); 

if (!$con) {    
   die(mysql_error()); }  

mysql_select_db("users", $con) or die(mysql_error());  

$login = "SELECT * FROM `data` WHERE (`usrname` = '$usrname' AND `password` = '$password')"; 

$result = mysql_query($login);  


if (mysql_num_rows($result) == 1) {     

$_SESSION['logged_in'] = true;   
  header('Location: indexlogin.php');  

exit; 

} 

else {     
echo "Wrong username or password."; 

}  

?> 

But I still get the else statement, "Wrong username or password. Someone help plz!"

  • 写回答

7条回答 默认 最新

  • dsgsdg206050 2011-01-10 01:48
    关注

    It hashes the password so it is not save in clear text e.g mylongpassword becomes 9a995d3f6a3d69c1a9b4344bed4f2c87

    Select the hashed password using the db First

    $password_from_db = Select * from user where username='".$_POST['username']."'
    

    Then password from the ($_POST['password']) should be hashed first in PHP then compared to the valued stored in the DB

    <?       
       if (md5($_POST['password'])==$password_from_db){
          return true;
       }else{
          return false;
       }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP