Grow your CSS skills. Land your dream job.

PHP Contact Form SPAM!

  • # December 31, 2010 at 11:04 pm

    Hey all,
    I know this is one of those questions that gets asked all the time. I did some looking around and can’t really find a good tutorial on a basic “3+4=?” type of question for my contact form. Basically, can anyone point me to a well detailed tutorial on adding that basic type of question to a contact form? I’m getting bombarded daily and just want to add a simple question to mine. I know about recaptcha and adding it to my form was extremely simple, BUT there is no way to shrink it down. It will not fit in my form. I really don’t know any PHP, so I would need something pretty easy to follow. Thanks!

    # January 2, 2011 at 10:40 pm

    Does anyone, at least, care to share what they are using on their own sites?

    # January 3, 2011 at 2:35 am

    Use wufoo

    # January 3, 2011 at 3:29 am

    I could write a tutorial for you if you want. It includes basic PHP

    # January 3, 2011 at 2:45 pm

    Thanks Ricky, that would be awesome! Would you like to me to post up or send you my current form’s HTML and php?

    gno
    # January 7, 2011 at 10:45 pm

    Preeminent, theres a much simpler solution to your problem than implementing a captcha check. Recaptcha is nice, and easily styleable if you know your way around with php. But Recaptcha, or anything like it would be overkill.

    Most likely you are a victim of spam bots, the same kind of bots that we protect our blogs against with comment moderation, plugins and what not. Those spam bots are easily tricked;

    All it takes is to add a input field (input type=”text-ish) and hide it with css (display: none;). That field will be empty, whenever someone visits your page in a browser and submits a message. Because they cant fill it out, due to it being hidden. But said bots, wont view your site in a browser – they’ll look at the markup, searching for forms. They fill out anything they find and submits it.

    So if you just make sure that the check-input field is empty before you send the email to yourself, you can avoid the spam.

    The code needed to implement this simple solution:
    The HTML:

    The CSS:

    input.check { display: none; }

    The PHP: (just wrap your current script in this if statement)

    < ?php
    if ($_POST == '') {
    // YOUR SEND-EMAIL-SCRIPT GOES HERE
    }
    ?>

    I hope that it will be useful for you!

    # January 8, 2011 at 10:05 am

    @gno:
    Actually pretty smart, never thought of that before.
    Of course I hate the fact that you use extra markup that no one sees…

    gno
    # January 8, 2011 at 11:24 am

    @SpeedGun:
    You’re right – its not semantically beautiful. But as long as you have a site that is small enough to not become target to special designed spam bots, I consider this a better solution than captcha checks. You gain some usability and sleekness, with the only downside being slightly less semantic code. I can live with that. :-)

    (To make it more semantic, one might make the input field of the hidden type instead. However, that would not trick the spambots to fill data into the field.)

    # January 13, 2011 at 12:53 am

    Fantastic Gno! I can’t wait to try this out! I REALLY appreciate your time on this!!

    # January 13, 2011 at 10:59 pm

    Ok, well when I added it to my form script, I throw an error. Here is what my script starts with, and I think that’s what is throwing an error: < ?php session_start();

    if(!$_POST) exit;

    Rest of the form script goes here...........and then ends with:
    }
    ?>

    So there is two instances of }
    ?>
    I use dreamweaver cs5 and it says there is a syntax error. How can I modify this so that it will work? Thanks so much for your help.

    # January 14, 2011 at 12:34 am

    In other words, when I add your recommended code, it makes it two instances of }?>at the very end.

    gno
    # January 14, 2011 at 3:35 am

    < ?php and ?> is PHP open and close tags. They only need to be wrapping php code – they can be opened and closed as many time as you with – you can even omit the closing tag in the end of the file, but there may never be two of the same following each other.

    To implement the php check you should:

    1) change the following line

    if (!$_POST) exit;

    into the next line

    if ($_POST == '') {

    2) add a closing } at the end of the file before the php closing tag.

    Rob
    # January 14, 2011 at 7:34 am

    There are loads of tuts out there already tbh, as gno said Recatpha is pretty awesome. Plus you help store books in digital form when you use it! :D

    http://www.google.com/recaptcha

    # January 15, 2011 at 7:28 pm

    Oh, I agree about recaptcha! I used it, and it works great, but I need something that will fit in a pretty small contact form for this particular project. The recaptcha can not be modified this small. Thanks Gno, I will go try this now!

    # January 15, 2011 at 8:15 pm

    Hey guys, ok I updated my form with this, but now I get this error when trying to send.
    “Fatal error: Call to undefined function isEmail() in /homepages/37/d221555405/htdocs/contactprocess.php on line 43″
    And line 43 is this:
    } elseif(!isEmail($email)) { which is part of this:} elseif(!isEmail($email)) {
    $error .= '

  • You have entered an invalid e-mail address.
  • ';
    }

    Now I don’t know for sure that this was caused by the spam check,but the form was working fine before. I’m hoping someone has a theory on why this error is happening now.
    Thanks a lot guys!

Viewing 15 posts - 1 through 15 (of 26 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".