php contact form sending blank emails sometimes

前端 未结 1 1219
忘掉有多难
忘掉有多难 2021-01-17 06:06

I\'ve gone thru tons of the forms and cant seem to find the answer. I have been working on this problem with my php form on and off for days. hope to find help here. the for

相关标签:
1条回答
  • 2021-01-17 06:43

    Add validation to the PHP, else even if no values was sent via POST, just by visiting the page its going to send a blank email. Most likely a search engine or such bot is just crawling.

    So check its POST

    <?php 
    if($_SERVER['REQUEST_METHOD']==='POST'){
    //put code here
    }
    ?>
    

    and check your values are set min-max length ect

    <?php 
    
    ...
    ...
    ...
    
    //Comments
    if(empty($_POST['comments'])){
        //comments empty, do or set something
    }else if(strlen($_POST['comments']) < 5){
        //not long enough, do or set something
    }else if(strlen($_POST['comments']) > 50){
        //too large, do or set something
    }
    ?>
    

    and most importantly check email is really an email..

    <?php 
    if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
     //is an email
    }else{
     //not an email
    }
    ?>
    

    Also your want to add a basic captcha else your be enjoying 1000s of marketing/spam emails per day.

    Good luck, implementing it.

    0 讨论(0)
提交回复
热议问题