PHP Tutorial: Integrating Captcha Validation in Your PHP Website

The contact form is one of the common features of most websites. Now, simple contact forms work just fine, but if you want to make them nicer, use AJAX when you create a simple PHP contact form with captcha validation to submit the form data.

One of the best benefits you get from AJAX is, there is no page load. And it is easy-to-integrate to any PHP and/or HTML website.

This is all ok, but why you should create with a captcha PHP validation?

First, there are so many competitors on the market right now who want to spam you continuously. And to avoid that spamming, it is necessary to include captcha validation.
So in this PHP tutorial, we’ll make one sample webpage to demonstrate how to implement PHP captcha validation in AJAX contact form or in simple language, we can say how to implement captcha.

Let’s Get Started

First, create a new PHP file and name it as index.php.

Now, write one image tag to show captcha in the contact form.

<form id="contact" action="javascript:void(0)" method="post" onsubmit="submit_form()">

    <h3>Contact Form</h3>


        <img src="captcha.php?rand=<?php echo rand(); ?>" id="captchaimg" height="50" width="350">



        <input placeholder="Captcha" type="text" tabindex="2" id="captcha_code">

        <span class="err captcha-err"></span>



        Can't read the above code? <a class="ccc" href="javascript:void(0);" onClick="refresh_captcha();">Refresh</a>



        <button name="submit" type="submit" id="contact-submit">Submit</button>



Now in the image tag, load a captcha library file that will generate captcha on the contact form using PHP functions.

<img src="captcha.php?rand=<?php echo rand(); ?>" id="captchaimg" height="50" width="350">

Take one link to refresh a captcha image, if it isn’t readable to the user.

<a class="ccc" href="javascript:void(0);" onClick="refresh_captcha();">Refresh</a>

After that, it’s time to validate captcha on AJAX call. Create one js file to check whether the captcha is valid or not.


    method: "POST",

url: "thank_you.php",

data: { captcha: captcha_code}


Next, create another PHP webpage and name it as thankyou.php

In this page, we’ll check the captcha with session value and make the response with either true or false.

$response = 1;

if(empty($_SESSION['captcha_code']) || strcasecmp($_SESSION['captcha_code'], $_POST['captcha']) != 0)


    $response = 0;


echo $response; exit;

If it’s successful, it will redirect the user to thank you page with the message.


If not, it will ask you to re-enter the captcha.

Want to Create a Web Application?

Validate your app idea and get a free quote.



If you face any problem, you can contact our PHP developers for help.

However, this was just a simple demo. You can build even more advanced contact form by using AJAX contact form builder.

It is designed to build custom contact forms and it’s a light version of AJAX form pro.

So, if you want to build an advanced contact form using AJAX for your website, you can hire PHP developer from us.

Grab a free copy of AJAX Captcha Validation demo from Github.

In case, if you still have any query or confusion regarding PHP captcha form or how to add captcha to PHP form,  then you can get in touch with us through our contact us form. One of our sales representatives will revert to you within 16 hours. The consultation is absolutely free of cost.

Author Bio
Jigar Mistry
Jigar Mistry
Designation: Vice President (Technologies)

Jigar Mistry is Vice President (Technologies) at Space-O Technologies. He has 14+ years of experience in the web and mobile app development industry. He has expertise in different mobile app categories like health and fitness, eCommerce, and on-demand.