0

Possible Duplicate:
How can I convert ereg expressions to preg in PHP?
Escape string to use in mail()

I am trying send my form, but I get this error...

Deprecated: Function eregi() is deprecated

I tried replacing it with preg_match(), but no luck. Here is my code:

$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>';
    }

}
Community
  • 1
  • 1

1 Answers1

1

It returns a slightly different value than eregi but if I'm reading your code correctly you should be able to use the strpos() function to determine if a substring exists in a string. Eregi ignores case so you might have to combine this with a strtolower($string) call too.

Something like this: if (strpos("\r",strtolower($name)) || strpos("\n",strtolower($name)))

user1903020
  • 187
  • 5
  • Ok I tried what you said with both $name and $mail_from. I don't get any errors when I send the form, and I get my validation once the forms sent. But I don't get the mail when I check my email. – sour_grapes Dec 15 '12 at 06:25
  • Have you confirmed that the PHP `mail()` function generally works on your server? – Barmar Dec 15 '12 at 07:12
  • Wouldn't it work if I sent it locally? Or do I have to be connected to a server? I am not sure about this part. – sour_grapes Dec 15 '12 at 08:51