【问题标题】:Form sending mail despite validation errors表单发送邮件,尽管验证错误
【发布时间】:2011-04-09 14:42:03
【问题描述】:

我刚刚构建了我的第一个带有验证功能的 PHP 联系表单,但无论如何它都在发送电子邮件。我肯定错过了什么。有人能看出来吗?

<?php if($_SESSION['instance'] == '1') {

    $email = $_POST['ENQemail'];
    $firstname = $_POST['ENQfirst_name'];
    $lastname = $_POST['ENQlast_name'];
    $message = $_POST['ENQmessage'];
    $secword = $_POST['ENQsecword'];



  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    $ERRemail = 'invalid email address';
  }
  if (strlen($firstname < 2)) {

    $ERRfirstname = 'Please enter your first name';
  }
  if (strlen($lastname < 2)) {

    $ERRlastname = 'Please enter your surname';
  }
  if (strlen($message < 50)) {

    $ERRmessage = 'Your message must be at least 50 characters';
  }
  if ($secword == $_SESSION['instance']) {

    $ERRsecword = 'Your security word did not match the image';
  }

  else {

      $to = "enquire@divethegap.com";
  $subject = "DTG Enquiry - ".$firstname." ".$lastname ;
  $message = $message;
  $headers = "From: ".$firstname." ".$lastname." ".$email. "\r\n" .
             "Content-type: text/html" . "\r\n";

  mail($to, $subject, $message, $headers);


  }
    }
?>

有什么想法吗?

【问题讨论】:

  • 旁注:请注意,“^[^@]{1,64}@[^@]{1,255}$”不会验证每个有效的电子邮件地址。例如,某人的地址包含多个 @ 符号(在用户名部分中转义,如 \@)、域部分中的 ipv4 或 ipv6 地址、转义的特殊字符等。你不能用简单的一行来验证电子邮件,因为它的形式是非常复杂。查看this link 了解更多信息,并遵循本页提到的 RFC 标准。

标签: php forms validation


【解决方案1】:

包含电子邮件发送代码的else 语句仅与上一个 if 语句相关联。这意味着它不会被调用的唯一时间是$secword == $_SESSION['instance'] 的计算结果为false。其他验证检查是否成功并不重要。

一种策略是通过将发生的所有错误存储在一个数组中来跟踪它们。如果数组为空,那么您就知道所有字段都正常并且可以安全地发送电子邮件:

$errors = array();
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
  $errors[] = 'invalid email address';
}
if (strlen($firstname < 2)) {
  $errors[] = 'Please enter your first name';
}
//...

if (count($errors) == 0){
  //send the email
  mail(...);
} else {
  //display the error messages
}

否则您的代码看起来相当不错!以下是一些其他建议:

  • 为了在 PHP 中使用会话,您 必须先致电session_start() 功能。这应该是您的 PHP 脚本所做的第一件事。

  • ereg() 函数 已在最新版本的 PHP 中弃用。这意味着该函数可能会在 PHP 的未来版本中被删除。建议您改用preg_match() 函数(注意,使用 preg_match(),正则表达式字符串必须 以/ 字符开头和结尾)。

【讨论】:

    【解决方案2】:

    底部是固定版本的逻辑。你错过了“else if”。

    此外,以下行看起来应该检查不等式,但这取决于您的代码,因此您会更好地了解:

    if ($secword == $_SESSION['instance'])
    

    这里是完整的修复:

        <?php if($_SESSION['instance'] == '1') {
    
        $email = $_POST['ENQemail'];
        $firstname = $_POST['ENQfirst_name'];
        $lastname = $_POST['ENQlast_name'];
        $message = $_POST['ENQmessage'];
        $secword = $_POST['ENQsecword'];
    
    
    
      if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
        $ERRemail = 'invalid email address';
      }
      else if (strlen($firstname < 2)) {
    
        $ERRfirstname = 'Please enter your first name';
      }
      else if (strlen($lastname < 2)) {
    
        $ERRlastname = 'Please enter your surname';
      }
      else if (strlen($message < 50)) {
    
        $ERRmessage = 'Your message must be at least 50 characters';
      }
      else if ($secword == $_SESSION['instance']) {
    
        $ERRsecword = 'Your security word did not match the image';
      }
    
      else {
    
          $to = "enquire@divethegap.com";
      $subject = "DTG Enquiry - ".$firstname." ".$lastname ;
      $message = $message;
      $headers = "From: ".$firstname." ".$lastname." ".$email. "\r\n" .
                 "Content-type: text/html" . "\r\n";
    
      mail($to, $subject, $message, $headers);
    
    
      }
        }
    ?>
    

    【讨论】:

    • 好吧,如果你不这样做,那么它会在检查第一个字段并且不标记下一个字段后停止
    • 在这种情况下,您应该使用我在上面的答案中描述为选项#2 的方法。
    • 嗨罗宾,如果你想一起显示所有“错误”,那么你需要使用一个标志变量,就像 no.good.at.coding 建议的那样。
    【解决方案3】:

    您正在独立地进行所有验证,如果安全图像正确,则无论其他所有内容是否通过检查,您都会发送电子邮件,因为您发送电子邮件的 else 与 @ 匹配987654322@

    您可以执行以下操作之一:

    1. 将所有ifs,但第一个更改为elseifs,这样只有当所有检查通过时,才会输入最后一个else(发送电子邮件)

      if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
          $ERRemail = 'invalid email address';
      }
      elseif (strlen($firstname < 2)) {
          $ERRfirstname = 'Please enter your first name';
      }
      elseif (strlen($lastname < 2)) {
          $ERRlastname = 'Please enter your surname';
      }
      elseif (strlen($message < 50)) {
          $ERRmessage = 'Your message must be at least 50 characters';
      }
      elseif ($secword == $_SESSION['instance']) {
          $ERRsecword = 'Your security word did not match the image';
      }
      else {
        $to = "enquire@divethegap.com";
        $subject = "DTG Enquiry - ".$firstname." ".$lastname ;
        $message = $message;
        $headers = "From: ".$firstname." ".$lastname." ".$email. "\r\n" . "Content-type: text/html" . "\r\n";
      
        mail($to, $subject, $message, $headers);
      }
      
    2. 设置一个标志,指示是否有任何检查失败。最后,只有在一切顺利的情况下才发送电子邮件:

      $isValid = true;
      
      if(invalidEmail){
          $isValid =false;
      }
      
      if(invalidName){
          $isValid =false;
      }
      
      //finally send mail if all validation passed
      if($isValid) {
         //send email
      }
      

    【讨论】:

      猜你喜欢
      • 2014-03-29
      • 2015-01-03
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 2015-02-15
      • 2018-08-24
      • 1970-01-01
      相关资源
      最近更新 更多