PHP Contact form with a Captcha Based on Math

PHP Contact form with a Captcha Based on Math

I’ve been working on this contact form with a custom captcha for a while now. If you are on github I’ll do a new repository soon.

Ideally this form is for anyone is building a contact form from scratch and wants to have a BASIC security against bad robots.

For this contact form i use two files. The first file called captcha.php and it generates the image with the math. The second file with the source code i call it index.php but it can be named anything you like. Also make sure you are using PHP 5.1 or later.

You can download the file here.

Comments

13 responses to “PHP Contact form with a Captcha Based on Math”

  1. hussain says:

    Hi,

    Amazing, Thank you for sharing.

    Thanks

    • Chris says:

      Thank you Hussain. Again this is not as good as google captcha but it works well against bad robots/spams. The code is clean & simple and you customize it easy. Cheers

  2. Russ says:

    Hi,

    I insert my email address into the field $sendto = “*******@*****.it”; in index.php
    but it doesn’t work.
    Somebody can help me? šŸ™

    • Chris says:

      Make sure you are developing to an online server and not locally. That’s the first thing I can think of. If that doesn’t work I’ll have a closer look to my code. Thanks

  3. Jake says:

    Great job on this tut! I am developing locally using WordPress, all seems to be fine except there seems to be an issue with the displaying on the actually image. What displays next to the math input field is just a broken image file. Any thoughts?

    At first thought I assumed it was because of being developed local, but I transferred to hosting site and same thing.

    • Chris says:

      Thanks Jake. The approach for installing this captcha in to wordpress is different. The code is meant for handcoding sites and not for CMS’s. There is great plugins out there you can use like contact-form-7. If I have time one day maybe i’ll make a tutorial of how to install this form in to wordpress.

  4. Dan says:

    It’s worth noting that the math part of the solution won’t hold up well against targeted attacks. Math-based captchas can easily be bypassed, as I demonstrate here:

    http://www.calcatraz.com/blog/math-based-captcha-security-1551

    So the only real security against a targeted attack is the difficulty of extracting text from the image.

    But if you’re looking to block untargeted attacks only (which will account for a lot of the attacks most sites see), the captcha given above should work well.

    • Chris says:

      If anyone wants real security, install google recaptcha. As being said this form is a very basic contact form for beginners or designers. Thanks Dan.

  5. Dariush says:

    thanks for great code and work fine with non-English language.

  6. Dariush says:

    how i can show errors in front of each input?
    thanks

Leave a Reply