简体   繁体   English

PHP 使用正则表达式进行表单验证

[英]PHP form validation using regex

Need some assistance with PHP form validation that uses regular expressions.在使用正则表达式的 PHP 表单验证方面需要一些帮助。 If they inputs are not valid, the user should be redirected back to the html form.如果他们的输入无效,则应将用户重定向回 html 表单。 My regex statements checkout when entered into ( https://regex101.com/ ), but the program is redirecting to the html form no matter the input.我的正则表达式语句在输入( https://regex101.com/ )时结帐,但无论输入如何,程序都会重定向到 html 表单。 Here is my work so far:这是我到目前为止的工作:

<?php

$data = $_POST;

if (trim($data['username'] == '') ||
    empty($data['password']) ||
    empty($data['verify_password']) ||
    empty($data['email'])) {
    
    echo "Please fill all required fields!";
    header("Location:index.html");
    exit;
}

if($data['password'] !== $data['verify_password']) {
  echo "Passwords do not match!";
  header("Location:index.html");
  exit;
}

// username must be at least 8 characters long, start with a letter, end in a number, and have no special symbols.
if (!preg_match('^(?=.*[A-Za-z])[A-Za-z\d]{8,}?[0-9]+$', $username)) {
  header("Location:index.html");
  exit;
}

// password must be at least 8 characters long, contain at least one lower case letter, one upper case letter,
// one number, and at least one special character ( . , / ? * ! ).
if (!preg_match('^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[.,\/?*!])[A-Za-z\d.,\/?*!]{8,}$', $password)) {
  header("Location:index.html");
  exit;  
}

// email must be at least 15 characters long, contain only one ( @ ) symbol, be a domain of gmail.com or bcit.ca, 
// user portion must start with a letter, can end in a number or letter, can contain a ( . ) or ( + ), not feature any 
// other special characters, and is case-insensitive.
if (!preg_match('^[a-zA-Z0-9_.+-]+@(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z0-9]+\.)?(gmail|bcit)\.(com|ca)$', $email)) {
  header("Location:index.html");
  exit;  
}

?>
<html>
    <div>
        <span>Welcome, </span><span><?php echo $data['username']; ?></span>
    </div>
</html>

You have syntax errors.您有语法错误。 if (trim($data['username'] == '') is missing a closing bracket and should if (trim($data['username']) == '') if (trim($data['username'] == '')缺少右括号,应该if (trim($data['username']) == '')

First error I saw.我看到的第一个错误。 There may be more.可能还有更多。 Turn on error reporting in your php.ini and you should see where the errors are moving forward.在您的 php.ini 中打开错误报告,您应该会看到错误正在向前发展。

You should not echo anything before header sent if there are no buffering.如果没有缓冲,您不应该在发送 header 之前回显任何内容。

This shall not be working:这将不起作用:

echo "Something";
header("Location:index.html");

Instead, you should do sth like that:相反,你应该这样做:

header("Location:index.html");
echo "Something";

First $username,$email,$password veriables are not defined as you're using them.第一个 $username,$email,$password 在您使用它们时未定义。 Secondly Your regex must have ending parameter(^) as you have in the start.其次,您的正则表达式必须像一开始一样具有结束参数(^)。 see the complete solution blow.查看完整的解决方案。

<?php

  $data = $_POST;
 $username = $data['username']; 
 $password = $data['password']; 
 $email = $data['email']; 
if (trim($data['username'] == '') ||
empty($data['password']) ||
empty($data['verify_password']) ||
empty($data['email'])) {
echo "Please fill all required fields!";
header("Location:index.html");
exit;
}
if($data['password'] !== $data['verify_password']) {

echo "Passwords do not match!";
header("Location:index.html");
exit;
}
// username must be at least 8 characters long, start with a letter, end in a 
   number, and have no special symbols.
 if (!preg_match('^(?=.*[A-Za-z])[A-Za-z\d]{8,}?[0-9]+$^', $username)) {
 header("Location:index.html");
 exit;
 } 
 // password must be at least 8 characters long, contain at least one lower case 
   letter, one upper case letter,
 // one number, and at least one special character ( . , / ? * ! ).
  if (!preg_match('^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[.,\/?*!])[A-Za-z\d.,\/? 
  *!] {8,}$^', $password)) {
    header("Location:index.html");
   exit;  
  }
 // email must be at least 15 characters long, contain only one ( @ ) symbol, be 
   a domain of gmail.com or bcit.ca, 
 // user portion must start with a letter, can end in a number or letter, can 
 contain a ( . ) or ( + ), not feature any 
 // other special characters, and is case-insensitive.
if (!preg_match('^[a-zA-Z0-9_.+-]+@(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z0-9]+\.)? 
 (gmail|bcit)\.(com|ca)$^', $email)) {

 header("Location:index.html");
 exit;  
}

?>
  <html>
  <div>
    <span>Welcome, </span><span><?php echo $data['username']; ?></span>
  </div>
  </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM