php中mysql的密码确认检查

r6l8ljro  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(331)
<?php
include("../../config/database_connection.php");

if( isset($_POST) )
{
    $user_name   =$_POST['user_name'];
    $email   = $_POST['email'];
    $user_pass_init    = $_POST['password'];
    $user_pass_conf = $_POST['passconfirm'];
    $full_name = $_POST['full_name'];
    $gender = $_POST['gender'];

    if ($user_name!="" && $email!="" && $full_name!="" && $gender!="") {
        if ($_POST['password']!= $_POST['passconfirm']) {
            header("location:../index.php?err= password do not match");
        }else{
            $query   = "INSERT into admin_users (user_name,email,user_pass,full_name,gender) VALUES('" . $user_name . "','" . $email . "','" . md5($user_pass_init) . "','" . $full_name . "','" . $gender . "')";
            $success = $conn->query($query);
        }

        if (!$success) {
            die("Couldn't enter data: ".$conn->error);
        } else{
            header("location:../index.php");
        }
    } else{
        header("location:../index.php?err= Enter all the fields");
    }
} else{
    header("location:../index.php?err= couldnot enter data");
}
?>

这是我试图执行的代码,以检查两个密码是否匹配。。。但当输入相同的密码我得到的密码值不匹配的问题是什么?帮我解决

piok6c0g

piok6c0g1#

我的建议是,这可能无法完全解决您的问题,但会使您的代码更加安全,因为您的代码容易受到sql注入的影响:

<?php
include("../../config/database_connection.php");
if( isset($_POST) )
{
    $user_name  = $conn->real_escape_string($_POST['user_name']);
    $email   = $conn->real_escape_string($_POST['email']);
    $user_pass_init    = $conn->real_escape_string($_POST['password']);
    $user_pass_conf = $conn->real_escape_string($_POST['passconfirm']);
    $full_name = $conn->real_escape_string($_POST['full_name']);
    $gender = $conn->real_escape_string($_POST['gender']);

    if (!empty($user_name) && !empty($email) && !empty($full_name) && !empty($gender)) {
        if ($user_pass_init != $user_pass_conf) {
            header("location:../index.php?err= password do not match");
        }else{
            $user_pass = md5($user_pass_init);
            $query   = "INSERT into admin_users (user_name,email,user_pass,full_name,gender) VALUES('$user_name', '$email', '$user_pass', '$full_name', '$gender')";
            $success = $conn->query($query);
            if (!$success) {
                die("Couldn't enter data: ".$conn->error);
            } else{
                header("location:../index.php");
            }
        }
    } else{
        header("location:../index.php?err= Enter all the fields");
    }
} else{
    header("location:../index.php?err= couldnot enter data");
}
?>

我也建议使用 password_hash() 而不是 md5()

相关问题