Form validation with Captcha image

In this tutorial I will show you how to create a captcha image to protect your form submissions.

Nowadays the spammers make more and more trouble for site administrators as they submit spams to forums, guestbooks, and registration forms and so on. To protect ourselves against spam robots it’s a good solution to extend our forms with a Captcha image. This image contains a security code which is only readable for humans and not for robots.

In this tutorial I will show you how you can generate such images on the fly with PHP and how to integrate it into your own form. First of all we need to split the task into 2 parts.

We need a PHP code which generates an image with a random code

We need a form where we check security code during processing.

Let’s begin with the image generation. If you think about it a bit we have to fulfill the following requirements:

Randomly generated code.

Randomly generated background image with some noise.

Randomly positioned characters.

So first create a background image. You can create it using the imagecreate function and pass the width and height as parameter. It is quite simple.

Afterwards we add some criss-cross lines to the background. If you increase the number of execution the background will be more complex. For this task we can use imageline function which draws a line from point Ax,Ay to point Bx,By with a color C. To make it always different we generated all coordinates with mt_rand functions. The color is created with the imagecolorallocate function. The code part looks like this:

Now it’s time to add a random security code to the image.To do this we first create a variable which contains all available characters. Later we will select a character one by one from this string randomly as follows:

The very last step is to store the collected characters, our code in a session variable. Later during the form processing this variable will be used.

You can find the complete Captcha image generator code on the last page of this tutorial.

Form processing

The next big task is to create a form using the image we just generated and process the visitor inputs. As you may remember we have stored the security code in a session variable so we need to start a session first. As next step we can check if the form was submitted or not. If not then we can display a form. Here you can create as complex form as you want. The only important thing is that you need to add an input text field for the security code and of course you need to display the Captcha image. This last step is very simple; just use our first PHP – which generates the image - code as image source. The form should look like this:

Now back to the form processing part of the script. First we need to check the existence of the security code and if it was submitted then we convert it to lowercase to make it case insensitive. If you want to make a case sensitive Captcha solution then you also need to modify the image generation script and remove the strtolower function.

It’s time to check if the security code we have saved in the session is the same as the security code typed by the visitor.If it is valid then we clean the session variable and set a result variable. This result variable will show us if we need to display the form again or not. You need to call your own business logic inside the valid case.