0

假设我有一个简单的表格:

<form action="register.php" method="post">
<label>Password:</label>
<?php if (isset($errors[1])) echo $errors[1]; ?> <- Displays error message if there is one
<input type="password" name="user_pass" />
<label>Confirm Password:</label>
<input type="password" name="user_pass_confirm" />

<input type="submit" />
</form>

$user_pass = $security->encrypt($_POST['user_pass']);
$user_pass_confirm = $security->encrypt($_POST['user_pass_confirm']);

$registration->form($user_pass, $user_pass_confirm);

还有一个类:

if (empty($user_pass)) {
$errors[1] = 'Passwords  required';
} else if ($user_pass != $user_pass_confirm) {
$errors[1] = 'Passwords don't match';
}

//if error array empty, create member

我要做的是验证密码以使其成为必需,确保它们匹配,我还将添加一个 preg_match 正则表达式以确保它至少有 8 个字符长或其他任何东西。

我的问题是,我在提交之前已经加密了密码(我认为不应该发布未加密的密码,如果我错了,请纠正我)。

然后当我的班级获得编码字符串时,我无法做任何事情来验证它。我可以通过将字段与什么都没有的加密/加盐版本进行比较来检查是否为空,但我确信这不是这样做的方法。

任何人都可以指出验证密码的标准程序或您对解决方案的任何建议。

非常感谢

4

1 回答 1

1

PHP 不能在客户端执行。由于 PHP 是一种服务器端语言,因此您无法使用 PHP 加密纯密码而不将其发送到服务器。必须先将密码发送到服务器,然后才能访问它。您可以通过 https 使用 SSL/TLS 等机制,但这不会影响您的 PHP 代码。

这意味着:在提交表单之前,您不能使用 PHP 加密密码。这可以通过 JavaScript 等客户端编程语言来完成。您可以实现一个 JavaScript 函数来检查密码是否正常(不是空的并且足够长/足够安全),然后让 JavaScript 对其进行加密,然后将其发送到服务器,以便将加密的密码传输到服务器。

<form action="register.php" method="post">
...
</form>
<?php
//$user_pass = $security->encrypt($_POST['user_pass']);
//$user_pass_confirm = $security->encrypt($_POST['user_pass_confirm']);
//$registration->form($user_pass, $user_pass_confirm);
//YOUR PASSWORD HAS NOT BEEN SENT TO YOUR SERVER YET. YOU CANNOT ACCESS IT USING PHP.
//YOU WOULD NORMALLY DO SOMETHING LIKE THIS (IN REGISTER.PHP)
if(isset($_POST["name_of_your_submit_field"])) //WHICH MEANS THAT YOUR FORM HAS BEEN SUBMITTED NOW
{
    //This checks whether the POST-variable of your submit field is set.
    //If it is, you know that the client has submitted the form and sent the form data to your server.
    //Only here you can access the form data.
    $user_pass=$_POST['user_pass'];
    if(strlen($user_pass) > 8)
    {
        $user_pass = $security->encrypt($user_pass);
        $user_pass_confirm = $security->encrypt($_POST['user_pass_confirm']);
        $registration->form($user_pass, $user_pass_confirm);
    }
}
?>
于 2012-10-29T01:39:39.820 回答