简体   繁体   中英

PHP Contact form Validation?

I have a website with a contact form, when I submit the details I get an error message about deprecated eregi() on line. This is the block of code that seems to be having a problem. I don't know php, so could anyone give a hand?

if (email_is_valid($youremail) && !eregi("\r",$youremail) && !eregi("\n",$youremail) && $yourname != "" && $yourmessage != "" && substr(md5($user_answer),5,10) === $answer) {
              mail($to,$subject,$message,$headers);
              $yourname = '';
              $youremail = '';
              $yourmessage = '';
              echo '<p style="color: #200041; text-align: center;">'.$contact_submitted.'</p>';
            }

Eregi is deprecated, so remove it from your code:

if (email_is_valid($youremail)&& $yourname != "" && $yourmessage != "" && substr(md5($user_answer),5,10) === $answer) {  
     mail($to,$subject,$message,$headers);
     $yourname = ''; 
     $youremail = ''; 
     $yourmessage = ''; 
     echo ''.$contact_submitted.'

You can forget about it, because it only check for new line signs.
If you want to keep new line checking, replace eregi with the preg_match function (but read the manual about it).

eregi() is deprecated. So you can use preg_match()

And your code will be

<?php
    if (email_is_valid($youremail) && $yourname != "" && $yourmessage != "" && substr(md5($user_answer),5,10) == '$answer')
    {
        mail($to,$subject,$message,$headers);
        $yourname = '';
        $youremail = '';
        $yourmessage = '';
        echo '<p style="color: #200041; text-align: center;">'.$contact_submitted.'</p>';
    }
    else
    {
        echo '<p style="color: red; text-align: center;">Error</p>';
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM