doulan4939 2011-01-26 10:38
浏览 47
已采纳

PHP MD5创建用户表单

I have used a tutorial here: http://www.phpeasystep.com/phptu/26.html to create a login form for my website. I have set the uPassword field in my database to be md5 and all of the passwords in the database are encrypted with md5.

The login works perfectly, however I am slightly confused about creating a registration form.

The form requests for a user to input their desired password. I am slightly confused as to how I will then take the password that the user inputs, converting it to md5 and then inputting the md5 password into the uPassword field in the user table.

Below is the code that I have for the processresgistration.php file:

/* Database connection info*/
mysql_select_db("dbname", $con);

$encryptedpassword = md5($_POST['uPassword']);

md5($uPassword);

$sql="INSERT INTO users (uName, uPassword, uSurname, uFirstName)
VALUES
('$_POST[uName]','$encryptedpassword','$_POST[uSurname]','$_POST[uFirstName]'";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Account created.  You can now login";

mysql_close($con)
?>

The code above is supposed to:

  • Create a variable named encryptedpassword
  • Use uPassword as encryptedpassword
  • Convert encrypted password to MD5
  • Input the MD5 password into the users table as uPassword

I'm sure that I've not used a correct variable somewhere, or I have done a simple error with my syntax; any comments/help is greatly appreciated!

Thanks, Chris M

  • 写回答

3条回答 默认 最新

  • doo6568 2011-01-26 10:58
    关注

    You code require some validation & escaping, more like this :

    <?php
    
    /* Database connection info */
    mysql_select_db("dbname", $con);
    
    if ($_REQUEST['METHOD'] == 'POST') {
        $uName = filter_input(INPUT_POST, 'uName');
        $uPassword = filter_input(INPUT_POST, 'uPassword');
        $uSurname = filter_input(INPUT_POST, 'uSurname');
        $uFirstName = filter_input(INPUT_POST, 'uFirstName');
    
        // do some validation here ...
    
        // if everything OK, then crypte the password
        $hashedPassword = md5($uPassword);
    
        // and store it
    
        $sql = sprintf(
            'INSERT INTO users (uName, hashedPassword, uSurname, uFirstName)
             VALUES (%s, %s, %s, %s);',
                mysql_real_escape_string($uName, $con),
                mysql_real_escape_string($hashedPassword, $con),
                mysql_real_escape_string($uSurname, $con),
                mysql_real_escape_string($uFirstName, $con)
        );
    
        if (!mysql_query($sql,$con)) {
            die('Error: ' . mysql_error());
        }
    
        mysql_close($con);
        echo "Account created.  You can now login";
    }
    
    ?>
    

    Now for login

    <?php
    
    if ($_REQUEST['METHOD'] == 'POST') {
        $uName = filter_input(INPUT_POST, 'uName');
        $uPassword = filter_input(INPUT_POST, 'uPassword');
        $hashedPassword = md5($uPassword);
    
        $sql = sprintf(
            'SELECT * FROM users WHERE uName = "%s" AND hashedPassword = "%s" LIMIT 1',
                mysql_real_escape_string($uName, $con),
                mysql_real_escape_string($hashedPassword, $con),
            );
    
        // etc etc ...
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错