How to make captcha on php

How to create a safe captcha on php by generating random characters and comparing them with user input? Example!

Creating a Captcha with PHP

Captcha stands for Completely Automated Public Turing test to tell Computers and Humans Apart. It is a type of challenge-response test used in computing to ensure that the response is only generated by humans and not by computers.

In this tutorial, we will look at how to create a captcha with PHP. We will be using the GD library which is a part of the PHP language.

First, let's create a PHP file called captcha.php which will contain our code. We will start off by declaring some constants:

define('WIDTH', 200);
define('HEIGHT', 50);
define('LINES', 3);
define('DOTS', 100);
define('FONT', 'fonts/arial.ttf');
define('FONT_SIZE', 18);

These constants define the width and height of the captcha image, the number of lines, the number of random dots, the font to be used, and the font size.

Now let's create a function which will generate the captcha code. This function will generate a random string of letters and numbers:

function generateCode() {
    // code
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $code = '';
    for($i = 0; $i < 5; $i++) {
        $index = rand(0, strlen($chars)-1);
        $code .= substr($chars, $index, 1);
    return $code;

The generateCode() function takes no parameters, and returns a random string of 5 characters. Now let's create a function which will generate the captcha image:

function generateImage($code) {
    // code
    $image = imagecreatetruecolor(WIDTH, HEIGHT);
    $background_color = imagecolorallocate($image, 255, 255, 255);
    imagefilledrectangle($image, 0, 0, WIDTH, HEIGHT, $background_color);
    // Generate noise
    for($i=0; $i < LINES; $i++) {
        $x1 = rand(0, WIDTH);
        $y1 = rand(0, HEIGHT);
        $x2 = rand(0, WIDTH);
        $y2 = rand(0, HEIGHT);
        imageline($image, $x1, $y1, $x2, $y2, imagecolorallocate($image, rand(0,255), rand(0,255), rand(0,255)));
    for($i=0; $i < DOTS; $i++) {
        imagesetpixel($image, rand(0, WIDTH), rand(0, HEIGHT), imagecolorallocate($image, rand(0,255), rand(0,255), rand(0,255)));
    // Generate text
    $text_color = imagecolorallocate($image, 0, 0, 0);
    imagettftext($image, FONT_SIZE, 0, 10, HEIGHT-10, $text_color, FONT, $code);
    // Output image
    header('Content-type: image/png');

The generateImage() function takes one parameter, which is the captcha code. It creates an image with the given code and random lines and dots. It then outputs the image as a PNG file.

Finally, let's create the main function which will generate the captcha:

function captcha() {
    // Generate code
    $code = generateCode();
    // Generate image
    // Return code
    return $code;

The captcha() function will generate a random code, create an image with that code, and then return the code so it can be stored in a session variable for later validation. To use this function, we just need to call it like this:

$code = captcha();

And that's it! You now have a basic captcha system using the GD library in PHP.

Answers (0)