我有多么高效地使用filter_var($ email,FILTER_VALIDATE_EMAIL)。?

时间:2015-12-07 07:23:20

标签: php email

我已经尝试了下面的一些代码,但我错过了一个主要的事情是电子邮件验证...但现在我想在我的代码中添加filter_var($email, FILTER_VALIDATE_EMAIL)但是在我的代码中包含它的地方很困惑。

我可以知道如何在代码中添加电子邮件验证。

请帮帮我,求求你原谅我错了。     这里是/*.php*/代码

<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();

// json response array
$response = array("error" => false);
if (!empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['mobile'])){
    // receiving the post params
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $mobile = $_POST['mobile'];

if ($db->isUserExisted($email)) {
        // user already existed
        $response["error"] = true;
        $response["error_msg"] = "User already existed with " . $email;
        echo json_encode($response);
    } else {
    // create a new user
    $user = $db->storeUser($fname, $lname, $email, $password, $mobile);
    if ($user) {
        // user stored successfully
        $response["error"] = false;
        $response["uid"] = $user["id"];
        $response["user"]["fname"] = $user["fname"];
        $response["user"]["lname"] = $user["lname"];
        $response["user"]["email"] = $user["email"];
        $response["user"]["created_at"] = $user["created_at"];
        $response["user"]["updated_at"] = $user["updated_at"];
        echo json_encode($response);
    } else {
        // user failed to store
        $response["error"] = true;
        $response["error_msg"] = "Unknown error occurred in registration!";
        echo json_encode($response);
    }
    }
} else {
    $response["error"] = true;
    $response["error_msg"] = "Required parameters are missing!";
    echo json_encode($response);
}
?>

2 个答案:

答案 0 :(得分:1)

你可以这样做:

  if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
     if ($db->isUserExisted($email)) {
        //code goes here
     }
  } else {
         // msg displaying invalid email
  }

答案 1 :(得分:0)

filter_var使用指定的过滤器过滤变量。这是它的工作原理:

$email = "john@doe.com";

if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo("{$email} is a valid email address");
} else {
    echo("{$email} is not a valid email address");
}

现在,您的代码应该是这样的:

<?php
    require_once 'DB_Functions.php';
    $db = new DB_Functions();

    // json response array
    $response = array("error" => false);
    if (!empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['mobile'])){
        // receiving the post params
        $fname = trim($_POST['fname']);
        $lname = trim($_POST['lname']);
        $email = trim($_POST['email']);
        $password = $_POST['password'];
        $mobile = trim($_POST['mobile']);

        // validate your email address
        if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
            // valid email address
            if ($db->isUserExisted($email)) {
                // user already existed
                $response["error"] = true;
                $response["error_msg"] = "User already existed with " . $email;
                echo json_encode($response);
            } else {
                // create a new user
                $user = $db->storeUser($fname, $lname, $email, $password, $mobile);
                if ($user) {
                    // user stored successfully
                    $response["error"] = false;
                    $response["uid"] = $user["id"];
                    $response["user"]["fname"] = $user["fname"];
                    $response["user"]["lname"] = $user["lname"];
                    $response["user"]["email"] = $user["email"];
                    $response["user"]["created_at"] = $user["created_at"];
                    $response["user"]["updated_at"] = $user["updated_at"];
                    echo json_encode($response);
                } else {
                    // user failed to store
                    $response["error"] = true;
                    $response["error_msg"] = "Unknown error occurred in registration!";
                    echo json_encode($response);
                }
            }
        } else {
            // invalid email address
            $response["error"] = true;
            $response["error_msg"] = "invalid email address";
            echo json_encode($response);
        }
    } else {
        $response["error"] = true;
        $response["error_msg"] = "Required parameters are missing!";
        echo json_encode($response);
    }
?>

旁注:除电子邮件地址外,您还应验证并清理所有输入字段。 See this answer