doupi1532 2012-05-28 08:12
浏览 48
已采纳

使用PHP,MySQL和MD5创建登录

Issue has been resolved. Thank you to all whom posted and helped me out. The column type was to short (varchar(25)) - I extended it and was able to login. Everyone's comments were appreciated.

I am having some issues when I try to log in using a test account I created. I am trying to make my own CMS. Right now I have the register page complete and working on the login page. I have MD5'd the user's password on register. When I go to try and login using this account the hash generated on the login page NEVER matches the hash stored in my SQL database.

Here is my PHP code and MySQL query:

$username = mysql_real_escape_string($_REQUEST['username']);
$password = mysql_real_escape_string($_REQUEST['password']);

$query = mysql_query("SELECT username FROM records WHERE username='".$username."'") or die(mysql_error());  
$result= mysql_fetch_array( $query );

if($result[0] == $username) {

        $query = mysql_query("SELECT password FROM records WHERE username='".$username."'") or die(mysql_error());
        $result = mysql_fetch_array( $query );
        $passHash = md5($password);

        echo "Hash: ".$passHash."<br/>";
        echo "User's hash: ".$result[0];

        if($result[0] == $passHash) {
            echo "XXXXX: WIN";
        }
        else {
           echo "fail";
        }

I have an account called "alex" the MD5 that was generated on the register page is "1c13465e24d91aca4d3ddaa1b" the MD5 hash generated on the login page is "1c13465e24d91aca4d3ddaa1bc3e7027"

Here is my registration page code:

$username = mysql_real_escape_string($_REQUEST['username']);
$password = mysql_real_escape_string($_REQUEST['password']);
$md5_pass = md5($password);
mysql_real_escape_string(mysql_query("INSERT INTO records (username, password, ) VALUES('$username', '$md5_pass' ) ") or die(mysql_error()));  

What am I doing wrong? I have been searching on here and looked at:

md5 hash login with php and mysql

but no luck. Any help is greatly appreciated!

  • 写回答

2条回答 默认 最新

  • douzhang1115 2012-05-28 08:21
    关注

    Most likely the column that stores the password hash is too short, e.g. VARCHAR(25).
    Make it bigger: CHAR(32) or BINARY(16).
    See also:

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

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序