繁体   English   中英

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

[英]PHP form validation using regex

在使用正则表达式的 PHP 表单验证方面需要一些帮助。 如果他们的输入无效,则应将用户重定向回 html 表单。 我的正则表达式语句在输入( https://regex101.com/ )时结帐,但无论输入如何,程序都会重定向到 html 表单。 这是我到目前为止的工作:

<?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>

您有语法错误。 if (trim($data['username'] == '')缺少右括号,应该if (trim($data['username']) == '')

我看到的第一个错误。 可能还有更多。 在您的 php.ini 中打开错误报告,您应该会看到错误正在向前发展。

如果没有缓冲,您不应该在发送 header 之前回显任何内容。

这将不起作用:

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

相反,你应该这样做:

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

第一个 $username,$email,$password 在您使用它们时未定义。 其次,您的正则表达式必须像一开始一样具有结束参数(^)。 查看完整的解决方案。

<?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