【问题标题】:Add PHP email validation to a variable styled code将 PHP 电子邮件验证添加到可变样式代码
【发布时间】:2013-09-19 08:21:44
【问题描述】:

简单地说,我找到了验证电子邮件的代码:

filter_var($user_email, FILTER_VALIDATE_EMAIL)

但不幸的是,我不知道如何将其添加到从表单获取信息的 PHP 代码中:

<?php $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";
    $recipient = "example@example.com";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>

我使用 Ajax 来获取表单的提交信息。

【问题讨论】:

    标签: php email-validation


    【解决方案1】:

    您可以使用filter_input 函数。此函数将检查 email 参数是否已在 POST 请求中发送,如果已发送,则将对其进行验证并返回经过验证的电子邮件或 FALSE。这样做:

    <?php 
        $email = filter_input( INPUT_POST, 'email', FILTER_VALIDATE_EMAIL );
        if ( $email ) {
            $name = $_POST['name'];
            $message = $_POST['message'];
            $formcontent="From: $name \n Message: $message";
            $recipient = "example@example.com";
            $subject = "Contact Form";
            $mailheader = "From: $email \r\n";
            mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
        } else {
            die("Email is ivalid!")
        }
    ?>
    

    【讨论】:

      【解决方案2】:

      你可以这样使用它:

      <?php
          if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
              $email = $_POST['email'];
              $message = $_POST['message'];
              $formcontent="From: $name \n Message: $message";
              $recipient = "example@example.com";
              $subject = "Contact Form";
              $mailheader = "From: $email \r\n";
              mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
      
          } else {
              header("Location: your_form.html");
      
          }
      

      如果您要将其插入数据库,最好使用mysql_real_escape_string 清理您的数据

      当电子邮件出错时,也会显示一条好消息。

      【讨论】:

        【解决方案3】:
        <?php 
        
            //if the form has been submitted
            if ( isset($_POST['submit']) ) {
                $name = $_POST['name'];
                $user_email = $_POST['email'];
                $message = $_POST['message'];
        
                //if user's email is not empty and is valid, send mail
                if ( !empty($user_email) && filter_var($user_email, FILTER_VALIDATE_EMAIL) ) {
                    $formcontent="From: $name \n Message: $message";
                    $recipient = $user_email;
                    $subject = "Contact Form";
                    $mailheader = "From: youremail@example.com \r\n";
                    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
                } else {
                    //show error
                    exit("Your email seems to be invalid.");
                }
            }
        
        ?>
        

        【讨论】:

          【解决方案4】:

          如果你需要一个好的 PHP 函数来检查输入是否是一个有效的电子邮件地址,你可以使用这个:

          function validateEmail($email) {
          $e = false;
          if ((trim ( $email )) == "") {
              $e = true;
          } elseif (strlen ( $email ) > 40) {
              $e = true;
          } elseif (! ((strpos ( $email, "." ) > 0) && (strpos ( $email, "@" ) > 0)) || preg_match ( "/[^a-zA-Z0-9.@_-]/", $email )) {
              $e = true;
          }
          return !$e;
          }
          

          如您所见,它将电子邮件长度限制为 40 个字符。

          【讨论】:

          • what you need is a good PHP function that checks that the input is a valid email address, -- 这正是 if(filter_var($email, FILTER_VALIDATE_EMAIL)) 所做的。
          • @AmalMurali 我很高兴听到这个 Amal
          • 你的功能比filter_var( $email, FILTER_VALIDATE_EMAIL )差很多,不要使用你的功能,也不要教任何人使用它。
          • 看看this article,然后检查你的函数可以验证什么。
          猜你喜欢
          • 2016-06-10
          • 2019-11-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-07
          • 1970-01-01
          • 2018-05-05
          • 1970-01-01
          相关资源
          最近更新 更多