php 确认密码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1622834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Confirm Password?
提问by bgadoci
New to all this so forgive my ignorance. I am trying to figure out how to add a "confirm your password" field to my form. Using PHP and mySQL. Is this entered in the html form code, and how can you set it to auto check that the password and confirm password fields match.
对这一切都很陌生,所以请原谅我的无知。我想弄清楚如何在我的表单中添加“确认您的密码”字段。使用 PHP 和 mySQL。这是在 html 表单代码中输入的,您如何将其设置为自动检查密码和确认密码字段是否匹配。
回答by Kaleb Brasee
Just get both the password and confirm password fields in the form submit PHP and test for equality:
只需获取表单中的密码和确认密码字段提交 PHP 并测试是否相等:
if ($_POST["password"] === $_POST["confirm_password"]) {
// success!
}
else {
// failed :(
}
where passwordand confirm_passwordare the IDs of the HTML text inputs for the passwords.
其中password和confirm_password是密码的 HTML 文本输入的 ID。
回答by GmonC
What you're trying to do is form validation. It's a good idea do validate on the client side (using javascript) so you have a faster response for your user on the interface, and on your server side (since your user can have javascript disabled - and because you should never blindly trustin user input. Read Should you do validation on your server sidefor some more information about this subject).
您要做的是表单验证。在客户端(使用 javascript)进行验证是一个好主意,因此您可以在界面和服务器端为您的用户提供更快的响应(因为您的用户可以禁用 javascript - 因为您永远不应该盲目信任用户输入。阅读您是否应该在服务器端进行验证以获取有关此主题的更多信息)。
You just need to compare the two posted values. If correct, insert in database. If not, dont do anything and returns a message to the user saying that the password is incorrect.
您只需要比较两个发布的值。如果正确,插入数据库。如果没有,请不要做任何事情并向用户返回一条消息,说明密码不正确。
I can't give more details since you didn't provide enough or detailed information of your php environment (frameworks used, libs used, etc).
我无法提供更多详细信息,因为您没有提供足够或详细的 php 环境信息(使用的框架、使用的库等)。
回答by Xinus
you can check it in JavaScript using
您可以使用 JavaScript 在 JavaScript 中检查它
<html><title></title><head>
<script>
function validate(){
if(!document.getElementById("password").value==document.getElementById("confirm_password").value)alert("Passwords do no match");
return document.getElementById("password").value==document.getElementById("confirm_password").value;
return false;
}
</script>
</head>
<body>
<form onSubmit="return validate()" action="nextPage.php">
Password: <input type="text" id="password" name="password" /><br/>
Reenter Password: <input type="text" id="confirm_password" name="confirm_password" />
<input type="submit" value="submit"/>
</form>
</body>
</html>
And on sever side you need to check it again in case client do not have JavaScript Enabled,
在服务器端,如果客户端没有启用 JavaScript,您需要再次检查它,
if($_GET['password']==$_GET['confirm_password'])
You have to use $_POST instead of $_GET in case of POST method
在 POST 方法的情况下,您必须使用 $_POST 而不是 $_GET
回答by Dharmendra
I updated the code, there is missing colon on form submit.
我更新了代码,表单提交上缺少冒号。
<html>
<title></title>
<head>
<script>
function validate(){
var a = document.getElementById("password").value;
var b = document.getElementById("confirm_password").value;
if (a!=b) {
alert("Passwords do no match");
return false;
}
}
</script>
</head>
<body>
<form onSubmit="return validate();" ">
Password: <input type="text" id="password" name="password" /><br/>
Re-enter Password: <input type="text" id="confirm_password" name="confirm_password" />
<input type="submit" value="submit"/>
</form>
</body>
</html>
回答by Ben Regenspan
Are you using some kind of framework? If not it should be as simple as checking after save that both fields are set and that $confirmationPassword == $passWord. Then apply whatever validation you need to the password before storing it in SQL.
你在使用某种框架吗?如果不是,它应该像保存后检查两个字段是否都已设置以及 $confirmationPassword == $passWord 一样简单。然后在将密码存储在 SQL 中之前,对密码应用您需要的任何验证。

