php中的注册表单
这是一个注册验证脚本。在我的注册表中有两个电子邮件和密码文件。第二个字段用于确认。我想检查用户是否在这两个字段中键入了相同的信息。如果我想在这个脚本中这样做,我应该使用另一个if语句吗?或者我应该用别的,如果?我对这一步感到困惑…在第二个if语句中添加附加条件。 e、 gphp中的注册表单,php,registration,Php,Registration,这是一个注册验证脚本。在我的注册表中有两个电子邮件和密码文件。第二个字段用于确认。我想检查用户是否在这两个字段中键入了相同的信息。如果我想在这个脚本中这样做,我应该使用另一个if语句吗?或者我应该用别的,如果?我对这一步感到困惑…在第二个if语句中添加附加条件。 e、 g 只需添加简单的检查。我不会将检查与常规密码检查结合起来,因为我可以想象,您会告诉用户出了什么问题 我会将松散错误的用户替换为如下数组: if ($password1 !== $password2) { // Add a
只需添加简单的检查。我不会将检查与常规密码检查结合起来,因为我可以想象,您会告诉用户出了什么问题 我会将松散错误的用户替换为如下数组:
if ($password1 !== $password2) {
// Add an specific error saying the passwords do not match.
}
你的代码有很多问题
$aErrors = array();
if ($password1 !== $password2) {
$aErrors[] = 'Another specific error!';
}
if (empty($password1) || empty($password2)) {
$aErrors[] = 'Another specific error';
}
if (empty($aErrors)) {
// Process the form!
}
所以
1. You are assinging $_POST['key'] = $somevalue, while I think you mean $somevar = $_POST['key']
2. Use an array for all error messages as it'll make your life a bit easier ..
3. To compare password use something like
if ($password1 !== $password2) {
}
所以你要检查一下像
$errors = array();
我还建议在查询中使用$\u POST值之前,先对其进行清理。
我希望这会有帮助。我想你的意思是:
if ($password1 !== $password2) {
$errors[] = 'Password dont match';
}
if(count($errors) > 0) { //if there are errors
foreach($errors as $err) {
echo $err.' <br />';
}
} else {
// whatever you want to do if no error
}
其次,这将使用错误数组:
$yourname = $_POST['yourname'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$country = $_POST['country'];
第三,如果只是一个建议,就使用嵌套的ifs
$errors = array();
一些评论:
您必须清理输入!看一看。
你的作业:而不是$\u POST['yourname']=$yourname;它应该是$yourname=$\u POST['yourname'];。
您正在使用许多变量进行错误控制,之后如果一切顺利,您只需忘记最后一个else块中的错误消息。对错误字符串使用某种数组,并使用它!
您确定您没有验证用户名/密码是否包含空格或奇怪字符,或者验证电子邮件是否有效?
一些示例代码…:
if (!empty($_POST['password1'])) {
if ($_POST['password1'] != $_POST['password2']) {
$errors[] = '<font color="red">The 2 passwords you have entered do not match.</font>';
} else {
$password = $_POST['password1'];
}
} else {
$errors[] = '<font color="red">Please provide a password.</font>';
}
快速提示:用数组$aErrors替换$error 1到6,并检查结尾处的$aErrors是否为空。正如小提示一样,变量赋值是错误的,在isset$\u POST['Registerme']之后,您将值赋值给$\u POST变量,除了本地vars.edit:-我还想给每个验证提供唯一的错误消息写答案花了太多时间,同时还有很多答案!
$errors = array();
if (!empty($_POST['password1'])) {
if ($_POST['password1'] != $_POST['password2']) {
$errors[] = '<font color="red">The 2 passwords you have entered do not match.</font>';
} else {
$password = $_POST['password1'];
}
} else {
$errors[] = '<font color="red">Please provide a password.</font>';
}
// Simple sanitize function, complete it
function sanitize_input ($inputstr) {
return trim(mysql_real_escape_string($inputstr));
}
if (isset ($_POST['Registerme']) {
// array of error messages to report
$error_messages = array();
$isvalid = true;
// Assignment
$yourname = sanitize_input ($_POST['yourname']);
$email = sanitize_input ($_POST['email']);
$email2 = sanitize_input ($_POST['email2']);
$password = sanitize_input ($_POST['password']);
$password2 = sanitize_input ($_POST['password2']);
$country = sanitize_input ($_POST['country']);
// Validation
if (empty ($yourname)) {
$error_messages[] = "You must provide an username";
}
if (empty ($password)) {
$error_messages[] = "You must provide a password.";
}
elseif ($password !== $password2) {
$error_messages[] = "Passwords do not match.";
}
// Same for email, you caught the idea
// Finally, execute mysql code if all ok
if (empty($error_messages)) {
// Execute mysql code
isvalid = true;
}
}
// After form processing, use isvalid which is false if there are errors
// and the error_messages array to report errors