How to make captcha in php

Learn how to create a captcha in PHP with a step-by-step example. Secure your forms from bots with this easy to follow guide.

Creating a captcha in PHP

A captcha, or Completely Automated Public Turing test to tell Computers and Humans Apart, is a type of challenge-response test used to ensure that a response is generated by a human and not a computer. Captchas are used to protect websites from automated programs, called bots, which can be used to do malicious activities such as spamming, creating fake accounts, etc. In this tutorial, we will be creating a captcha in PHP. Captchas can be created in many different ways, such as using text, images, audio, or even a combination of all three. For this tutorial, we will be using a combination of text and images.

Creating the Captcha Image

The first step in creating our captcha is to generate the captcha image. To do this, we will be using the GD library in PHP. This library allows us to generate images on the fly. We will start by creating a blank image. This can be done with the following code:
$image = imagecreatetruecolor(200, 50);
This code creates a blank image with a width of 200 pixels and a height of 50 pixels. Next, we will fill the image with a background color. This can be done with the following code:
imagefill($image, 0, 0, imagecolorallocate($image, 255, 255, 255));
This code will fill the image with a white background. Now we will generate the text for the captcha. This can be done with the following code:
$text = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'), 0, 6);
This code will generate a random string of 6 characters from the set of letters and numbers. Finally, we will add the text to the image. This can be done with the following code:
imagettftext($image, 20, 0, 10, 30, imagecolorallocate($image, 0, 0, 0), 'fonts/font.ttf', $text);
This code will add the text to the image with a font size of 20 pixels and a black color.

Displaying the Captcha Image

The next step is to display the captcha image. To do this, we will use the header() function in PHP. This function allows us to send HTTP headers to the browser. We will be sending a header telling the browser to display the image:
header('Content-type: image/jpeg');
Next, we will output the image data to the browser. This can be done with the following code:
imagejpeg($image);
Finally, we will clean up the image data from memory. This can be done with the following code:
imagedestroy($image);

Validating the Captcha

The final step is to validate the captcha. To do this, we will need to store the text of the captcha in a session variable. This can be done with the following code:
$_SESSION['captcha_text'] = $text;
Now, when the user submits the form, we can check if the text they entered matches the text stored in the session variable. This can be done with the following code:
if ($_POST['captcha_text'] == $_SESSION['captcha_text']) {
    // captcha was entered correctly
} else {
    // incorrect captcha
}
And that's it! We now have a working captcha in PHP.

Answers (0)