比较两个密码PHP

时间:2013-09-23 20:02:23

标签: php

这是我的代码似乎工作正常,但它看起来不正确。 我知道我很容易受到注射。只想知道是否这个 是比较两个密码的最佳方法吗?

if (empty($_POST['password'])) {
    $errors[] = "Please enter a password";
} else {
    if ($_POST['password'] != $_POST['password1']) {
        $errors[] = "Your password did not match the confirmed password";
    } else {
        $p = $_POST['password'];
    }
}

2 个答案:

答案 0 :(得分:5)

你的代码很好,我只想写一点不同:

if (empty($_POST['password'])) {
   $errors[] = "Please enter a password";
} elseif($_POST['password'] !== $_POST['password1']) {
   $errors[] = "Your password did not match the confirmed password";
} else {
   $p = $_POST['password'];
}

答案 1 :(得分:0)

我建议你必须在开始时检查$_POST['password']$_POST['password1']

if (!empty($_POST['password']) && !empty($_POST['password1'])) {
        if (strcmp($_POST['password'], $_POST['password1']) === 0) {
            $p = $_POST['password'];
        } else {
            $errors[] = "Your password did not match the confirmed password";
        }
    } else {
        $errors[] = "Please enter a password";
    }