duanchifo2866 2017-04-11 11:03 采纳率: 100%
浏览 63
已采纳

mysql查询用散列密码更新数据库

I'm building a registration and log in form and I would like to hash my user passwords on registration and my main problem at this point is how to write the query to update the passwords when the user is signing his email and pass for the 1st time and I want to add mysqli_insert_id() to the query to follow the unique id's for each user.

so I have database named testdb with users inside.

my code is perfectly working until the moment when you have to query the password and update it.

First I'm hashing my passwords

$password = $_POST['password'];

$hashed_password = password_hash($password, PASSWORD_BCRYPT);

$query = "UPDATE `users` SET `password` = '$hashed_password' WHERE id = "

As you can see I have problem with my query which should update passwords in my DB.

I have this code written until this point so I need help to proceed my UPDATE query

if (array_key_exists("submit", $_POST) ) {

    // connect to our db
    $link = mysqli_connect("localhost", "root", "", "secretdi");
    // check for connection
    if ( mysqli_connect_error() ) {
        die("Database Connection Error");
    }

    $error = "";

    if ( !$_POST['email'] ) {
        $error .= "An email address is required<br>";
    }

    if ( !$_POST['password'] ) {
        $error .= "A password is required<br>";
    }

    if ( $error != "" ) {
        $error = "<p>There were error(s) in your form:</p>".$error;
    } else {

        $query = "SELECT id FROM `users` WHERE `email` = '".mysqli_real_escape_string($link, $_POST['email'])."' LIMIT 1";

        $results = mysqli_query($link, $query);

        if ( mysqli_num_rows($results) > 0 ) {
            $error = "That email address is taken.";
        } else { 

            $query = "INSERT INTO `users` (`email`, `password`) VALUES('".mysqli_real_escape_string($link, $_POST['email'])."','".mysqli_real_escape_string($link, $_POST['password'])."') ";

            if (!mysqli_query($link,$query)) {
                $error = "<p>Could not sign you up - please try again later</p>";
            } else {

                $password = $_POST['password'];

                $hashed_password = password_hash($password, PASSWORD_BCRYPT);


                $query = "UPDATE `users` SET `password` = '$hashed_password' WHERE id = "


                echo "Sign up successful";
            }

        }

    }
  • 写回答

1条回答 默认 最新

  • down_load1117 2017-04-11 11:16
    关注

    You should never, under any circumstances, store the password in plain-text.

    This means that when you register the user you should hash the password right away and store it in the hashed format when you perform the insert statement. Your current logic stores the password in plain-text, and if the registration is successful then you attempt to update the password to become the hashed one. This means that if anything fails you might end up with having only the plain-text password in your database, and either way it means that at a certain point it does exist there in plain-text at some given point for any user which we don't want.

    Besides, the update statement serves no purpose other than wasting a few extra lines of code.

    Something like this should do:

    if ( mysqli_num_rows($results) > 0 ) {
        $error = "That email address is taken.";
    } else { 
        $password = $_POST['password'];
        $hashed_password = password_hash($password, PASSWORD_BCRYPT);
    
        $query = "INSERT INTO `users` (`email`, `password`) VALUES('".mysqli_real_escape_string($link, $_POST['email'])."','".$hashed_password."') ";
    
        if (!mysqli_query($link,$query)) {
            $error = "<p>Could not sign you up - please try again later</p>";
        } else {
            echo "Sign up successful";
        }
    }
    

    If you really want to stick to your plan, however, then you can select the user id for the user you just created (e.g. by using the email identifier which is hopefully unique) and use it as such to identify which user to update. Please don't do that.

    Note: I recommend taking a look at mysqli's prepared statements to ensure a higher level of security instead of escaping individual variables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)