表单不发送,函数eregi()已弃用

时间:2012-12-15 05:51:35

标签: php forms send deprecated eregi

  

可能重复:
  How can I convert ereg expressions to preg in PHP?
  Escape string to use in mail()

我正在尝试发送我的表单,但我收到此错误...

  

不推荐使用:不推荐使用函数eregi()

我尝试用preg_match()替换它,但没有运气。这是我的代码:

$all_valid = $name_valid = $email_valid = $comments_valid = true;

if (isset($_POST['submit'])) {  

    if ($_POST['name'] == '') {
        $all_valid = $name_valid = false;
    }

    if ($_POST['comments'] == '') {
        $all_valid = $comments_valid = false;
    }

    if (!$validator->check_email_address($_POST['email'])) {
        $all_valid = $email_valid = false;
    }

    if ($all_valid) {
        //  ####    NO PROBLEMS FOUND - PROCESS THE FORM DATA HERE
        $mail_to = 'cat30@hotmail.com'; //  recipient address
        $subject = "Email from website";    //  email message subject line
        $name = mysql_real_escape_string(trim($_POST['name'])); //  sanitize the name
        if (eregi("\r",$name) || eregi("\n",$name)){    //  avoid email header injection
            die();
        }
        $mail_from = mysql_real_escape_string(trim($_POST['email']));   //  sanitize their email address
        if (eregi("\r",$mail_from) || eregi("\n",$mail_from)){  //  avoid email header injection
            die();
        }
        $comments = htmlspecialchars(trim($_POST['comments'])); //  convert HTML characters into entities

        $headers = 'From: '. $mail_from. "\r\n";
        mail($mail_to, $subject, $comments, $headers);

        $response = '<h2>Thanks for contacting us, will get back to you soon</h2>';
    }

}

1 个答案:

答案 0 :(得分:1)

它返回一个与eregi略有不同的值,但如果我正确读取你的代码,你应该能够使用strpos()函数来确定字符串中是否存在子字符串。 Eregi忽略大小写,所以你可能必须将它与strtolower($ string)调用结合起来。

这样的事情:     if(strpos(“\ r \ n”,strtolower($ name))|| strpos(“\ n”,strtolower($ name)))