Prevent php web contact form spam

前端 未结 7 2141
面向向阳花
面向向阳花 2021-01-30 14:14

I am an amateur web designer, I have searched on stackoverflow.com and other websites and have found many fixes for this issue I\'m having, but none of them have worked (probabl

7条回答
  •  面向向阳花
    2021-01-30 14:48

    If the spam you're getting does not have a comment, why not simply add a check for that? There's absolutely no reason for a real, human visitor to submit your contact form without a comment.

    Since you don't want to add a captcha, the easiest solution in the short term would be to check that the comment is a minimum number of characters and contains at least a certain number of words.

    For example:

    $comments = trim($_POST['comments']); // trim() to strip off whitespace from beginning and end, like spaces and linebreaks
    
    if (strlen($comments) < 20 || substr_count($comments, " ") < 3) {
        died('Your comment is too short.');
    }
    

    This is a very simple check to see that the comment contains at least 20 characters and at least 3 spaces (4 words). Tweak as needed.

提交回复
热议问题