Different ways to implement CAPTCHA in PHP

Before talking about the implementation of CAPTCHA, we are just looking into a basic introduction of CAPTCHA and its need.

A CAPTCHA (an acronym for “Completely Automated Public Turing test to tell Computers and Humans Apart”) is a type of challenge used to determine whether the user is a human or a computer.

It is useful in web forms to prevent automatic submission by a program.

Generally, we create an image having some text by using PHP functions or jQuery and the same text we store in session so that on form submission, we can check that the verification text is same as drew on image or not.

If there is a requirement to refresh the CAPTCHA image then you have to update text on a session as well.

There are some method to create CAPTCHA in PHP

  1. Using GD and Image functions of PHP
  2. Using jQuery plugin
  3. Using jQuery AJAX
  4. Using google reCaptcha.


  1. Using GD and Image Functions of PHP-

    PHP provides some GD functions which helps in image processing and can create a image or can write some text on it. For reference of the available functions you may visit http://php.net/manual/en/book.image.php

    There is a simple example of creating CAPTCHA using GD functions.

    1. Create a php file ‘session.php’ containing HTML part and a form in which CAPTCHA is to implement.

    2. Create another PHP file “index.php”


    3. Add a captcha.php file-


  1. Using jQuery plugins

    Creating CAPTCHA is much simple by using jquery plungins.

    There are number of plugins available like

    For this what you have to do –

    Just download a plugin and include it in your page where a form is implemented after jquery. Then use those plugin’s function to add CAPTCHA in an input field using selectors. It will automatically add a image of CAPTCHA besides the input field and will create a session containing the same text which is being shown in an image.

    For example,

    I am taking a plugin named realPerson

    1. Just download the zip file of plugin form http://keith-wood.name/realPerson.html
    2. Create your HTML page having form element in which an input field.
    3. Include jquery, plugins css and plugin


     3. Using jQuery AJAX-

You can also use an ajax request to refresh the CAPTCHA image.

For this, you need to hit an ajax request into a file where you can update new captcha code on a session and then create an image with a new captcha code.

In front-end, you have to change the “src” attribute, so that image can be changed according to the new code which is saved in the session.

File containing HTML form

you will need to add jquery code to refresh captcha by clicking on it.


  1. Using Google’s reCaptcha-

    Google provides CAPTCHA which is a free service that protects your site from spam and abuse. For this, you will need reCaptcha API key.

    It’s a simple and reliable solution.

    It provides a library for PHP https://developers.google.com/recaptcha/old/docs/php?hl=en

    You can download and use it by providing API key in the code.


Read out about Solid Principles >>


Please follow and like us:

Leave a Reply