How to make captcha php

Learn how to create a secure captcha in PHP with a step-by-step example. Improve user experience & protect your website from bots.

Creating a Captcha in PHP

A captcha is a type of challenge-response test used in computing to determine whether or not the user is human. It is often used to protect websites and web applications from automated abuse such as spam, brute-force attacks, and other malicious activities. In this tutorial, we will learn how to create a captcha in PHP.

The first step is to create a folder for our project. Inside the folder, create a file called index.php. This file will contain the HTML code and PHP code for our captcha.


<html>
    <head>
        <title>Captcha in PHP</title>
    </head>
    <body>
        <h1>Captcha in PHP</h1>
        <form method="post" action="index.php">
            <p>
                <label for="captcha">Please enter the code shown below:</label>
                <br />
                <img src="captcha.php" />
                <br />
                <input type="text" name="captcha" />
                <br />
                <input type="submit" value="Submit" />
            </p>
        </form>
    </body>
</html>

The above code creates a basic HTML form with a text field and a submit button. The text field will be used to enter the captcha code. The captcha image is generated by the captcha.php file which we will create next.


<?php
session_start();

// Set the content-type
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(200, 50);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 200, 50, $white);

// The text to draw
$text = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

// Replace path by your own font path
$font = 'font.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);

$_SESSION['captcha_text'] = $text;
?>

The above code creates a captcha image with a random string of characters. The string is then stored in a session variable. Finally, the captcha image is outputted as a PNG image.

The next step is to validate the captcha code entered by the user. We do this by comparing the code entered by the user with the code stored in the session variable. If the codes match, then the user is allowed to proceed, otherwise an error message is displayed.


if(isset($_POST['captcha']) && $_POST['captcha'] == $_SESSION['captcha_text'])
{
    // Allow the user to proceed
}
else
{
    // Display an error message
}

And that's it! We have successfully created a captcha in PHP. Captchas are an important part of any website or web application and are a great way to protect it from automated abuse.

Answers (0)