PHP - 过滤电子邮件地址

时间:2015-04-09 12:18:20

标签: php validation email

我有一个注册脚本,我希望只有具有特定电子邮件的用户才能注册。 (例如:所有电子邮件必须以&#34结尾; @ test-test.com")

这是我的代码:



if (isset($_POST['username'], $_POST['email'], $_POST['p'])) {
    $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $email = filter_var($email, FILTER_VALIDATE_EMAIL);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error_msg .= '<p class="error">Invalid e-mail Adress!</p>';
    }
&#13;
&#13;
&#13;

我知道我不能使用FILTER_SANITIZE_EMAIL ......任何人都可以告诉我该怎么做?提前谢谢。

6 个答案:

答案 0 :(得分:1)

我认为您应首先使用filter_var确保该电子邮件有效,然后查看其是否具有正确的域名,例如通过preg_match

function tellMeIfThisIsValid($email) {
    $allowedDomain = '@test-test.com';

    $isValid = FALSE;
    if (filter_var($email, FILTER_VALIDATE_EMAIL) !== false) {
        if (preg_match('/' . preg_quote($allowedDomain) . '$/', $email)) {
            $isValid = TRUE;
        }
    }
    printf("%-25s => %s" . PHP_EOL, $email, $isValid ? 'valid' : 'not valid');
}

tellMeIfThisIsValid('test@test-test.com');
tellMeIfThisIsValid('test.test@test-test.com');
tellMeIfThisIsValid('@test-test.com');
tellMeIfThisIsValid('test@test-test.com.se');
tellMeIfThisIsValid('test@anothertest-test.com');
tellMeIfThisIsValid('test@testtest.com');

输出:

test@test-test.com        => valid
test.test@test-test.com   => valid
@test-test.com            => not valid
test@test-test.com.se     => not valid
test@anothertest-test.com => not valid
test@testtest.com         => not valid

答案 1 :(得分:0)

试试这个

    <?php

    $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);

    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
          echo 'Invalid Email';
          die;
    }
    $allowedEmail = array(
                        'gmail.com',
                        'yahoo.com',
                        'stack.com',
                    ); // ALLOWED EMAIL DOMAINS SHOULD BE ADDED INTO THIS ARRAY.

    $emailArr = explode('@', $email);

    if(in_array($emailArr[1], $allowedEmail)) {
        echo 'allowed Email';
    } else {
        echo 'Not allowed Email';
    } 

?>

答案 2 :(得分:0)

试试这个 - 将检查电子邮件中的正确字符,如果包含格式正确的电子邮件地址:

$error = 0;
if($_POST['email']){
    if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
             '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $_POST['email'])){
                // your mail is incorrect.
                $error++;
    }
}

if($error == 0)
{
   // you have no errors, manipulate entered data
}

答案 3 :(得分:0)

您可以使用正则表达式

 $email = 'myname@sample.com';
 $regex = '/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@test\-test\.com$/';

 if(preg_match($regex, $email))
 {
    echo 'Valid';
 }
 else
 {
    echo 'Invalid';
  }

答案 4 :(得分:0)

非常感谢您的回答!这对我有用:

$check = "/^[_a-z0-9-.]*@test-test.comt$/i";
  $email = $_POST['email'];
  
  if (!preg_match ($check, $email))
  {
    $error_msg .= '<h4 class="sans-serif">Invalid Email adress!</h4>';
  }

答案 5 :(得分:-1)

您可以使用以下代码:

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    //valid
}

PHP版本:5&gt; = 5.2.0

http://php.net/manual/en/function.filter-var.php