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.

Requirement:

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.

HTML

JavaScript

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.

Explanation:

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.

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.

8 Comments

  1. 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.

  2. 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.

  3. 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 *