Create a Simple Captcha Script Using PHP

Demo Download

We all have encountered captcha validation in online web forms. Basically captcha are used to check that weather you are human or a bot. These captcha images contains some readable text with some noise, shape, distortion, lines and dots, which are readable by human and who can write it into the captcha input field to confirm that user is human, not a bot.

There are ready made third party captcha plugins available on the internet such as ReCaptcha, aim of this tutorial is to share knowledge that how captcha works and how can we create our own custom captcha.


Please make sure that GD(Graphics Draw) library is installed on your host. Mostly web host already have it. But if you don’t then you can install it, follow instructions or ask you web hosting company to install it.

Steps to create a Captcha Using PHP

  1. Create an index.php file
  2. Create a captcha.php file

1. Create an index.php file

First of all i will create an index file, in this file i will create a html form of captcha. I will also add javascript that will refresh captcha without refreshing page. After captcha form submission, entered captcha code will be validated with the generated captcha code. If both are same user will see a message of success otherwise failure.

Now create index.php page and copy paste the below html captcha form in it.



Add the above javascript in the footer of index.php. This script will refresh the captcha if it is very difficult to read, so that user can insert new captcha code.

PHP Script

Enter the below php validation script in the header of index.php file, before starting the <html> tag.

The above script is matching the captcha code with the users input and showing message of success or failure.

2. Create a captcha.php file

Now most important step, create a captcha.php file and copy paste the below script in it.

PHP Script

In the above code i include a font name monofont  $captcha_font = 'monofont.ttf'; you can use any font that you want otherwise you can download this font from here.

Make sure that you keep this font in the same folder where you are keeping index.php and captcha.php files.

Now you can browse index.php file on your local host or online web host if you have. Our captcha form is now ready to use.


I have used comments to explain each step in the above code, however i will also explain their working separately. Captcha.php file is performing the following actions to create a captcha:

  • Creating a blank image with white background
  • Creating random dots
  • Creating random lines
  • Creating random 6 letters on image

Creating a blank image with white background

Following script is generating a blank image with height 50px and width 130px.

Creating random dots

Following script is generating random dots in the image background.

Creating random lines

Following script is generating random lines in the image background.

Creating random 6 letters on image

Following script is generating random 6 letters and putting them on the captcha image.

Always destroy the image instance after creating captcha image, using  imagedestroy($captcha_image);

You may also noticed that i started  session_start();  in the beginning of the page because i need to store a random generated captcha value in session variable  $_SESSION['captcha'] = $captcha_code; to compare with the user input value for validation purpose.

Demo Download

If you found this tutorial helpful, kindly share it with your friends and developer groups.

Facebook Official Page: All PHP Tricks

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.


  1. Sir How To enables ,
    throw error..
    Fatal error: Uncaught Error: Call to undefined function imagecreate() in C:\xampp\htdocs\Omesh\sws2\captcha.php:31 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Omesh\sws2\captcha.php on line 31

  2. Hi Javed. My previous comment didn’t show the complete information. I implmented the Captcha on my site and it works perfect; great work! I really appreciated; however, it does not work if I set ” Header set Content-Security-Policy “script-src ‘self’ ” on my .htaccess file; it does not refresh. Do you now how could I solve this conflict? Thanks in advance.

  3. Hi Javed. I implmented the Captcha on my site and it works perfect; great work! I really appreciated; however, it does not work if I set <> on my .htaccess file; it does not refresh. Do you now how could I solve this conflict?

    Thanks in advance.

  4. I wasted so much time trying to figure out why my implementation accepted the form if they didn’t enter anything, only to now realize that your entire script does not work because it totally just accepts a blank entry and that is true even on your demo. What a huge waste of time.

    1. Well you can make the field required using html attribute or JavaScript. I was supposed to explain the captcha here therefore I didn’t explain the field required attribute here. Simply add required=’required’ in the captcha input field. Hope this help you out.

    2. You mean to tell me, that you couldn’t write in to the form process the proper code to stop the form from being submitted if the the captcha field was not filled in and did not equal the $_SESSION[‘captcha’]

      You need to go to school beother

  5. Good day, Thanks for the Captcha Tutorial and source code and can easily to understand, and I can use on my website, but one problem is the Font Directory, need to use ($captcha_font = dirname(__FILE__) . “./monofont.ttf”;) from ($captcha_font = ‘monofont.ttf’;) to correct the file path of font(font-family to use), I found this error when I remove the header and search for an alternative way for Font Dir path. thanks

  6. First let me say thank you for your time. I appreciate you sharing this knowledge.

    I first tested the captcha just as you described with a BLANK actin URL for the web form and it works just as you would expect. However, once I deployed it within a real web form using my URL for the action contact form, even setting the input captcha text filed as required, the form is sent successfully while completely ignoring IF the captcha is accurate or not.

    I don’t understand HOW to include this in a REAL web form where the captcha is not ignored and your code actually monitors if a human is processing the web form.

    I appreciate your help!

    1. Hi Jon, Captcha compares the code with the session value. Kindly make sure that session is enabled on your host. Sometimes sessions are not able to pass from one page to another, in this case contact your hosting provider.
      If session is not the issue, then you will need to debug the code.
      Simply print/echo the captcha value after form submission and also print the session value that is stored in session for captcha text validation.

  7. Hi,
    I am using you module in magento for custom form. Its working fine when I was using it on Apache server. But when I changed my server to nginx server it show me error Error filtering template: Notice: Undefined index: captcha for session variables. Please help me.

    1. Right click on the image and open in new tab to make sure if image is creating correctly or not. Also make sure that image library is enabled on localhost to generate image.

  8. Works fine in localhost but not in live site.
    Captcha image is not displaying just a thumb is there. Any idea?

    1. Follow the tutorial step by step and see the result, at first you must see the blank image, check the image URL in code inspector and copy paste the image URL in new tab of browser to check

  9. Hi
    Thanks for your post , this is exactly what i needed.
    How does the javascript get the new captcha without sending a new
    thanks in advanced

  10. Hi Javed,

    I have used your captcha module in different places and working well.

    Recently I deployed the same module on the newly configured Ubuntu 16.04 server which has PHP 5.6.40 installed.

    I copied your complete demo but it is also not displaying the numbers. Do not show any error message too.
    All works fine, but only TEXT not displaying. I checked the font file and everything as it is in your demo.

    I checked for php-gd library for 5.6 and installed it correctly. You can view the configuration from here:

    Could you please assist me? I searched a lot, but failed. What are dependencies that should be installed to run this function? I think some modules have missed.

    I look forward to hearing from you soon.

    1. I think that there is something font related issue, you may try with another font file. Or you enter random number by yourself, comment mt_rand() function and hard code any number for testing purpose.

  11. Hey, Numbers doesn’t show and I got this error:
    [01-Feb-2019 13:14:09 UTC] PHP Warning: imagettfbbox(): Could not find/open font in C:\inetpub\wwwroot\assets\captcha.php on line 89
    [01-Feb-2019 13:14:09 UTC] PHP Warning: imagettftext(): Could not find/open font in C:\inetpub\wwwroot\assets\captcha.php on line 101

  12. Hello sir, I tried to run your captcha using xampp latest version but the image cannot be display.

    I wonder if I should try to run it with older version of xampp or should I run with another application.

    Your help is much appreciate.

    1. HI Ran, i don’t think that this is xampp issue, may be you should try to create image using PHP, check are you able to create image in PHP?
      If you are not able to create image using php, better to look into this first. @imagecreate() i am using this function for this purpose. I have mentioned all steps to create image. Before captcha you should know how to create image using php.

  13. I love the awesome post! I read your posts often and I shared this post on my Facebook and my followers loved it.
    Keep up the awesome work.

  14. Hi sir I need your help badly
    I want this captcha on my website i need only captcha like this, please help me how can I add only captcha there? I am trying more than 4 months but no results I only learnt to make this type of captcha from you but when I tried to add this my site It shows nothing only missing image please help me to make a script for my site thank you you are only my last hope please help me asap please

    1. Dear Sumit, this is the error of image source, so you are providing incorrect source url, if image is not available on the provided source URL then you will get the same error, i will also suggest you to first implement my script on your machine and then implement it. Also try to keep Captcha script on same website, because you are using different website for captcha code, i never have experience of creating captcha on another website.

      1. Could you please help me to add captcha on this page, please?

        could you provide me with a good captcha validation code, please?

        site is here

        I need CAPTCHA for this site Please help

Leave a Reply

Your email address will not be published. Required fields are marked *