Add captcha to your form using PHP step by step

Captcha is very popular method to prevent automated software from performing actions which degrade the quality of service of a given system .You can add captcha to your form using php by following these steps-

1.Generate a random image to add captcha to your form using PHP-

In this step we are going to code a program which will generate an image which contains random characters.

I)-Start session and Define Variables-In the code given below $width ,$height,$ number  and $font represents

width of captcha image,height of image number of characters to show in captcha and relative address of font file.

 II)-Create a function to generate a random string- This function will return a code which will appear on captcha.

In this function $allchar contains a string of possible characters,do not add ‘ i’ ,’l’,’1′  and ‘0‘,’o‘ because they looks similar.

 III)-Create a blank image and choose colors-Use imagecreate() to generate a blank image and  choose the font size,background color ,text colors to add captcha to your form using PHP.

 IV)-Add random lines and ellipse to image-Although it is not necessary to add lines and ellipse but it will make more secure to your captcha.

V)-Add text to image-Now its time to write the text on image.

 VI)-Output the image-Set the appropriate headers to your file Content-Type will set type of file which is an image of jpeg format cache-control  and Pragma headers will prevent browsers and proxy servers to cache your captcha image.At the end of the code save the captcha code in a session variable  named captcha to check that code entered by user is correct or not.

Save the codes used in I to VI as image.php.

2-Create a form to add captcha to your form using PHP-

Create a simple form using HTML and after its submission by user check if it is same as code stored in session variable named captcha.

You can also add captcha to your form using PHP using true type fonts.To use a different font to need to replace value of $font to the relative location of your font file. Download the complete files from here to add captcha to your form using PHP.

Sample output:

add captcha to your form using php

add captcha to your form using php

Download Source 

  • jewel

    its not working man.

    • its working on my localhost.
      if you are getting any error please comment.

      • naqqash

        Undefined variable: POST in E:xampphtdocscaptcha

        • Add this line at beginning of each page.


          • naqqash

            i have done but still not working
            this is my code in process file. If there is any error plz let me know or change it.

            $uname = $_POST[‘uname’];
            $pass = $_POST[‘pass’];
            if($uname = ‘naqqash’ and $pass = ‘123’ and $_SESSION[‘captcha’]== $POST[‘captcha.php’]){
            echo “Allah is love”;
            else echo “not working”;

            when i execute the code there is a message “not working”.

          • replace $POST[‘captcha.php’] to POST[‘captcha’]

          • naqqash

            thank you so much it is working well

      • naqqash

        it is working well but i think the captcha image or text easily read or understandable. Have you any way to help me to generate some complicated text

        • Yes,It is simple but you can make it complicated by adding more noise ,belding characters etc or you can use third by captcha tool like “Recaptcha”.

          • naqqash

            Sir when i try to sign up to get recaptcha there is showing a text field named “domain”. But i want to use recaptcha on localhost. Then pl z help me that what should i written in domain named text field. or another way to get recaptcha

          • Write any domain name on that field.By default, all keys work on “localhost” (or “”).

  • luky


  • ritz

    not working