Create a CAPTCHA Challenge using PHP- 7 CAPTCHA Scripts

Create a CAPTCHA Challenge using PHP- 7 CAPTCHA Scripts

In our last tutorial(How to Create an Image Using PHP) we discussed how to create an image in PHP. Today let’s see how to use our knowledge from last tutorial to create something useful. In this tutorial I’m going to show you how to create a CAPTCHA challenge using PHP. I have designed 7 CAPTCHA scripts with a submit form.

7 CAPTCHA Scripts

I’m not going to explain every script line by line because we discussed most of these functions already. However there are few things you should keep in mind.

PHP Sessions

session_start();
CAPTCHA is a challenge which your visitors must solve. Hence there must be a way to know whether your submitted correct solution or not. To compare submitted solution with the correct solution, we store the correct one in a PHP session variable. Before we can store anything in a session, we must first start up the session. We do that using session_start();.
Note: The session_start(); function must use BEFORE any HTML tag.

$_SESSION["ttcapt"] = $text;
We store expected CAPTCHA solution in a session using $_SESSION[]. In these CAPTCHA scripts, I have used $_SESSION[“ttcapt”] = $text;. The session name is ttcapt and value is $text.

01: CAPTCHA with numbers only

CAPTCHA with numbers
This is a simple CAPTCHA challenge we use numbers only. It has 5 random numbers.

02: CAPTCHA with alphanumeric characters

CAPTCHA with alphanumeric characters
This script use numbers and characters with the length of 5.

03: CAPTCHA with alphanumeric characters drawn in TrueType fonts

CAPTCHA with alphanumeric characters drawn in TrueType fonts
You can use external TrueType font to make CAPTCHA more effective.

04: CAPTCHA with alphanumeric characters drawn in TrueType fonts with randomly drawn background design

CAPTCHA with alphanumeric characters drawn in TrueType fonts with randomly drawn background design
I have drawn randomly changing polygon design on the background. OCR recognizing ability of this picture is 0%. Best part is you can easily change the background and make CAPTCHA more challengeable.

05: CAPTCHA with alphanumeric characters drawn in TrueType fonts with external background

CAPTCHA with alphanumeric characters drawn in TrueType fonts with external background
I here I have used external backgrounds. You can add background as many as you want and they will be randomly picked.OCR recognizing ability of this picture is 0%.

06: CAPTCHA with a calculation challenge

CAPTCHA with a calculation challenge
Here is a simple mathematical calculation challenge. OCR recognizing ability of this picture is 0%.

07: CAPTCHA with alphanumeric characters with two colors

CAPTCHA with alphanumeric characters with two colors
This is a different type of challenge. I designed this with two text colors. Challenge is to recognize BLACK color text only and ignore red. With a small tweak you can randomly change the color too.

HTML form with CAPTCHA challenge

This is the PHP script to include mentioned CAPTCHAs. Change src="7.php" with file name to your CAPTCHA image.

Download CAPTCHA Scripts

You can download all CAPTCHA scripts with HTML form and backgrounds here. Font files are not included due to copyright issues. Use 02 TrueType font files and rename them as font1.ttf and font2.ttf or change the font name in script as necessary.

Comments
  1. Harry

    This is the best article to learn how to create a captcha in PHP in the web right now. Thanks man. It was very helpful.

    • Gamage AGM

      I’m really glad to hear that. But there are many other valuable articles about CAPTCHAs too.