duanhui1869 2011-10-20 16:36
浏览 64

如何验证电子邮件地址[重复]

Possible Duplicate:
How to validate an email address in PHP

I was wondering someone can help me please.

I need to validate the email address for the below code but am having problems.

<?php

if ($_POST) {

    $expected = array('name', 'email', 'emailmessage');
    $validation = array(
        'name'          => 'Please provide your full name',
        'email'         => 'Please provide your valid email address',
        'emailmessage'  => 'Please provide message'
    );

    $errors = array();
    $output = array();

    foreach($expected as $key) {

        $input = htmlspecialchars($_POST[$key]);

        if (array_key_exists($key, $_POST)) {
            if (empty($_POST[$key])) {
                $errors[$key] = $validation[$key];
            } else {
                $output[$key] = $_POST[$key];
            }
        } else {
            $errors[$key] = $validation[$key];
        }

    }

    if (!empty($errors)) {
        $array = array('error' => true, 'fields' => $errors);
    } else {

        // PROCESS FORM

    // ---------------------------------------------------------
    // BEGIN EDITING
    // ---------------------------------------------------------

    $to = "qakbar@hotmail.co.uk"; //This is the email address messages will be sent to
    $web_name = "My Test Web Form"; //This is the name of your website that will show in your email inbox

    //get IP address
    $ip = $_SERVER['REMOTE_ADDR'];

    //make time
    $time = time();
    $date = date("r", $time);

    // ---------------------------------------------------------
    // END EDITING
    // ---------------------------------------------------------

    $emailmessage = trim($emailmessage);
    $emailmessage = nl2br($emailmessage);
    $emailmessage = htmlspecialchars($emailmessage);
    $emailmessage = wordwrap($emailmessage, 70);

    //Visible form elements
    $name = $_POST['name']; //Sender's name
    $email = $_POST['email']; //Sender's email
    $emailmessage = htmlspecialchars($_POST['emailmessage']); //Sender's message

    //Setting up email
    $subject = "New Message from $web_name";

    $message = "
                New message from $name <br/><br/>

                Message:<br />
                $emailmessage
                <br/>

                <br/>
                Email: $email<br />
                IP:</strong> <span style=\"color:#990000;\">$ip</span><br />
                Date:</strong> $date

                ";

    $header  = 'MIME-Version: 1.0' . "
";
    $header .= 'Content-type: text/html; charset=utf-8' . "
";
    $header .= 'From:'. $email . " 
";

    $sent = mail($to, $subject, $message, $header);

        //$message = '<div id=message>You have successfully subscribed to our newsletter</div>';
        $array = array('error' => false, 'message' => $message);
    }

    echo json_encode($array);
}

I want the email to validate in the $validation array as my messages are passed through this and need the email validation to do the same.

I was trying to use the following but did not know where to place it or how to call it.

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

    // email is valid

} else {

    // email is invalid

}

Any help is much apprecaited.

Thank you

  • 写回答

3条回答 默认 最新

  • dongli5785 2011-10-20 16:51
    关注

    You could use it like this:

    filter_var($email, FILTER_VALIDATE_EMAIL)  or  die("Email wrong.");
    

    Right after you assigned this exact variable:

    $email = $_POST['email'];
    

    Of course this could be structured more sensible, and a nicer error notice would also be possible. But it sounds as if you need more general practice with PHP first.

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据