简体   繁体   English

将PHP电子邮件验证添加到变量样式的代码中

[英]Add PHP email validation to a variable styled code

Simply, I found this code to validate email: 简而言之,我找到了以下代码来验证电子邮件:

filter_var($user_email, FILTER_VALIDATE_EMAIL)

But unfortunately I don't know how to add this to my PHP code that get info from the form: 但是不幸的是,我不知道如何将其添加到从表单获取信息的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!");
?>

I used Ajax to get form's submitted information. 我使用Ajax来获取表单的提交信息。

You can use filter_input function. 您可以使用filter_input函数。 This function will check if email argument has been sent in the POST request and if it has been, then it will validate it and return validated email or FALSE. 此功能将检查POST请求中是否已发送email参数,如果已发送,则将对其进行验证并返回已验证的电子邮件或FALSE。 Do it like this: 像这样做:

<?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!")
    }
?>

You can use it like this: 您可以像这样使用它:

<?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");

    }

If you're gonna insert this to a database it'll be good to sanitize your data with mysql_real_escape_string 如果要将其插入数据库,最好使用mysql_real_escape_string清理数据

Also display a good message when the email is wrong. 当电子邮件错误时,还要显示一条好消息。

<?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.");
        }
    }

?>

If what you need is a good PHP function that checks that the input is a valid email address, you can use this: 如果您需要一个好的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;
}

As you see it limits email length to 40 characters. 如您所见,它将电子邮件长度限制为40个字符。

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

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