How to create captcha image with php

By | May 11, 2015

Implementing captcha image with php into your project is very simple to ensure maximum security. I am going to take us through the basic steps you need to take to fully implement image captcha in your website.

How to create captcha image with php

Steps in a nutshell:

  1. Generate random string of a desired length
  2. Assign the string to a session variable to know the expected captcha code
  3. Create an image with the generated random string on it
  4. Create your form and output the image on it.
  5. Lastly, validate the user’s input against the captcha session variable.

These basics steps are all you need to create a captcha image to your website.

Now we are going to be taking the steps one by one.

1. Generate random string of a desired length

You have to create a function that generates a random string to be used for your captcha image. Depending on what you need it might be a string of numbers, lowercase text, uppercase text or a mixture of the three.

I have created a function that does this so easily:

This function takes two arguments:

  • length – the number of characters you want with the default set to 10 characters, and
  • allcases – a boolean to specify if you want all cases and the default is set true

This function takes care of the step one. Now to step two.

2. Assign the string to a session variable to know the expected captcha code

You simply generate the string with the above function and assign it to a session variable just every time you are about to output a form that is to contain a captcha image:

This creates a random string of 8 characters with all cases, and assigns it to a session variable ‘captcha’.

3. Create an image with the generated random string on it

Here you need to get a background image first. This background image must be very good in lending with the intended font type and font color so as to limit OCR from reading the text in the image. So a background with patterns drawn on it with the intended font color is awsome.

Also you should have the .ttf file of your intended font and then create a PHP file that creates image.

A full detail on How to create images with PHP is here on my previous article on How to create images with PHP GD Library. (This link opens in a new window so you can follow this article)

4. Create your form and output the image on it.

After following the steps on the article to create a php file that creates the image, just add it to your form:

This above simple form includes an img tag that adds the captcha image to your form and provides an input element with name “captcha” to supply the data. Now you should note that the assigning of the captcha string to the session variable and outputting the image should be done regularly or just every time the need for a captcha image is needed to always keep the captcha code new.

As for the url you poing the src of the image to, you can use some re-write rules to make this better, for a better security effectiveness.

5. Lastly, validate the user’s input against the captcha session variable.

This is quite simple, but always make sure your code gets to this point before re assigning new random string value to the session variable.

So, THAT’S IT!

If there is anything unclear in this article feel free to comment it below.

Happy coding!!!

Leave a Reply

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