Create a Simple Captcha Code in PHP
This tutorial is for Create a Simple Captcha Code in PHP. This is the best way and very simple to build captcha code in PHP. Captcha most commonly uses your site if another person is not scraping or for other reasons depending on your requirement. In this example, I have displayed an image in captcha code.
In this tutorial, we will generate a unique CAPTCHA every time, a random number is generated using the rand() function in PHP which generates a random number between 0 to 61 and substr to get only 6 digits.
Create a Simple Captcha Code in PHP is allowing users to enter data into our website, and we need to check entered by the human. Otherwise, people will use robots to push the bulk and add dummy data into the website.
In this tutorial, we will use the PHP GD library to create our CAPTCHA. We will also have to write a little bit of code and create a random string to be written on the image.
#images.php
<?php session_start();
$images = imagecreate(110,60);
$green = imagecolorallocate($images,0,200,0);
$gray = imagecolorallocate($images,230,230,230);
imagefill($images,0,0,$gray);
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$var= "";
for($i=1;$i<=6;$i++) {
$var = $var.substr($str,rand(0,61),1);
}
$_SESSION['capital'] = $var;
imagettftext($images,20,0,9,29,$green,$var);
header("content-type:image/jpeg");
imagejpeg($images);
imagedestroy($images);
?>
#index.php
<img src="images.php" alt="images">