【问题标题】:checking password confirmation in php在php中检查密码确认
【发布时间】:2019-04-12 05:07:39
【问题描述】:
<?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");
}
?>

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

【问题讨论】:

  • var_dump($_POST) 显示什么?
  • if (!$success) 测试应该在您执行INSERTelse 块内。否则,$success 不会被设置。
  • 您输入所有字段的检查应包括密码字段。
  • 您的代码对 SQL 注入开放。见stackoverflow.com/questions/60174/…
  • 不要使用 MD5 加密密码。使用 PHP 的 password_hashpassword_verify

标签: php mysql forms confirmation password-confirmation


【解决方案1】:

这是我的建议,这可能无法完全解决您的问题,但它会使您的代码更安全,因为您的代码容易受到 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()

【讨论】:

猜你喜欢
  • 2015-05-18
  • 2013-11-05
  • 2017-05-13
  • 2011-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-29
  • 2018-10-09
相关资源
最近更新 更多