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 in PHP website.

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

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

hand icon 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">
        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>
Copy to Clipboard

hand icon 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=?>" id="captchaimg" height="50" width="350">
Copy to Clipboard

hand icon 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>
Copy to Clipboard

hand icon 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}
Copy to Clipboard

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

hand icon 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;
Copy to Clipboard

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


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

Want to Create a Web Application?

Need to validate your app idea or consult with an expert? Get a free consultation now!

Cta Image



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

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.

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

So, if you want to build an advanced contact form using AJAX for your website, or start a new project in PHP then you can get in touch with us. We are a PHP web development company with 10+ years of experience and have expertise in building scalable web applications in PHP for clients globally.

Apart from the captcha validation you can further improve the security of your web application by Google two-factor authentication login in PHP. Additionally, if you’re looking to improve user engagement on your website, you can explore adding browser push notifications. Check out our blog post on display browser push notification in PHP for a detailed tutorial on how to incorporate this feature into your PHP website.

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.

Bhaval Patel

Written by

Bhaval Patel is a Director (Operations) at Space-O Technologies. He has 20+ years of experience helping startups and enterprises with custom software solutions to drive maximum results. Under his leadership, Space-O has won the 8th GESIA annual award for being the best mobile app development company. So far, he has validated more than 300 app ideas and successfully delivered 100 custom solutions using the technologies, such as Swift, Kotlin, React Native, Flutter, PHP, RoR, IoT, AI, NFC, AR/VR, Blockchain, NFT, and more.