treehouse : what would you like to learn today?
Web Design Web Development iOS Development

PHP Contact Form SPAM!

  • 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!
  • Does anyone, at least, care to share what they are using on their own sites?
  • Use wufoo
  • I could write a tutorial for you if you want. It includes basic PHP
  • Thanks Ricky, that would be awesome! Would you like to me to post up or send you my current form's HTML and php?
  • 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:
    <input type="text" name="check" class="check" />

    The CSS:
    input.check { display: none; }

    The PHP: (just wrap your current script in this if statement)
    <?php
    if ($_POST['check'] == '') {
    // YOUR SEND-EMAIL-SCRIPT GOES HERE
    }
    ?>


    I hope that it will be useful for you!
  • @gno:
    Actually pretty smart, never thought of that before.
    Of course I hate the fact that you use extra markup that no one sees...
  • @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.)
  • Fantastic Gno! I can't wait to try this out! I REALLY appreciate your time on this!!
  • 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.
  • In other words, when I add your recommended code, it makes it two instances of }?>at the very end.
  • <?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['check'] == '') {


    2) add a closing } at the end of the file before the php closing tag.
  • 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
  • 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!
  • 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 .= '<li>You have entered an invalid e-mail address.</li>';
    }

    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!
  • isEmail() is not a function for PHP, as far as I know, so either you have to create the function first or you can try the function is_email() thats built into WordPress.
  • Bob is right. isEmail is a custom function, so you have most likely fucked something up when you attempted to implement my solution. It's probably an include or require call you have removed.
  • Sorry guys, I'm so clueless on php. I'm confused because I haven't edited my form at all except for what Gno suggested I do. So the isEmail() has always been part of the form and has been working fine. I'm not sure what I can do now. UGH! I thought I was close! Unfortunately I am using this form on another site, and now that email address is getting bombarded from a spambot as well. So I have to find something.
  • So I took out what you suggested Gno, and retested and the form works perfect. So your suggestion is clashing with the isEmail() part for some reason.
  • Ok here is a quick explanation of what just happened.

    PHP as you might have guessed has lot's of built in functions for example is_array() gives you a true and false answer if something is an array or not. But you can write your own functions for PHP - as that is how you create applications etc etc...

    So what someone has done with that script is create a function called isEmail() - which is not in PHP by default - and it is probably a regular expression match against if the email address given by the user is actually an email address or just a load of rubbish. If it is an email address you will get a true response, if not a false one. This will then give the answer to this part of your code.


    elseif(!isEmail($email)) { $error .= '<li>You have entered an invalid e-mail address.</li>'; }


    it reads:

    if *Email is not actually an email* then the variable "error" equals *line of HTML error message*

    So what has happened is somewhere along the line your isEmail() function has been removed or it's name has changed. Normally things like this are "included" - so you would have another file with more PHP in it that you include into the main body of PHP - it saves space and lets your functions be kept nice and tidy :D

    I would really recommend having a run through some basic PHP tutorials:

    http://blog.themeforest.net/screencasts/diving-into-php-video-series/

    This is good - if you go through them in about 2hrs you will have a massive jump on your understanding :)
  • @Preeminent, I can ensure you that there is no way, that my "solution" to your problem is clashing with your script.

    What is does is just to check if the field that we hide with css is empty. The problem is most likely your implementation of it.

    The code which is sending your email should just be wrapped by if ($_POST['check'] == '') { and }

    However, if you don't know how the script works, I think it would be a good idea to look at some php tutorial as Rob suggests. This is very basic PHP (just checking if a given variable has a value or not)
  • Gno, I'm certainly not saying that anything about your spam check is wrong. Hell, I wouldn't know anyway! But for some reason, with the addition of the spam check, an error occurs with that isEmail() line. And I put the check exactly where you told me to. It's just odd. But Rob, thanks for your time as well. I've got to try and find time to go over those videos, so I can get this to work. Because I really like your idea Gno, I want to try this out. Thanks again guys.
  • @Preeminent. You can try to email me the code if you want to, and I'll look over it to see if I can spot the flaw :-) My email address is in my profile on this forum.
  • Looks as if Gno's process works quite well! Still having issues with my personal site's contact form. Some spam coming from the same few IP's and servers, but I hope to get to the bottom of that issue soon. This form was being used on two different sites, and it looks like the spam has stopped completely from the other site! Thanks for your help Gno! Does anyone have any good tips on tracking down the origins of emails, by using the info in the headers? I've been doing some looking around on this issue. I've looked up the IPs and have emailed the companies who own the servers. What else can I do? Every single one of the emails getting through, look to be coming from the same person, just from a few different IPs.