CAPTCHA stands for Completely Automated Public Turing Test to Tell Computers and Humans Apart.
The use of web services has increased along with the chances of malicious robot attacks. Captcha system prevents websites from various bot attacks.
A good quality captcha system must have the following characteristics:
- Content can be easy to understand by human
- Suitable for all types of bots abuse
- Quick and less time consuming
- A high quality CAPTCHA must be strong and easily usable
Different types of captcha:
- Text-based Captcha
- Image-based Captcha
- Video Captcha
- Audio Captcha
- Puzzle-based Captcha
Here we will learn step by step to create a Login form with Captcha, for that we will create two files,
- Login.php
- Captcha.php
Step 1: Design Login Form With Captcha Code
First we will create a Login.php file and design a Login form using HTML with input to enter Username, Password and input to enter Captcha code and display Captcha code. We will call captcha.php to display captcha on image.
Login.php
Step 2: Create Captcha Code
We will create captcha.php to create Captcha code with image. We will store Captcha code into Session variable $_SESSION[‘captcha’] to use when validate captcha after submit. We will also define Captcha width, height, text color, font style etc. to design according to requirement. For changing font style you need to use any font files and provide the path of the file in following Script.
Captcha.php
- imagecreatetruecolor(int $width,int $height) function is used to create a new true-color image. This function returns a blank image of the given size.
- imagecolorallocate(resource $image,int $red,int $green,int $blue) function is used to set the color in an image.This function returns a color which is given in RGB format.
- imagefilledrectangle(resource $image,int $x1,int $y1,int $x2,int $y2,int $color) function is used to create a rectangle filled with a given color in the image.
- imagettftext (resource $image,float $size,float $angle,int $x,int $y,int $color,string $font_file, string $text) function is used to write text to the image using TrueType fonts.
$image : An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().
$size : Font size
$angle : Angle in Degrees
$x : X-Coordinate
$y : Y-Coordinate
$color : Text color
$font_file : Font style
$text : Text
- imagepng(resource $image,int $to,int $quality,int $filters) function is used to view an image in the browser,convert any other image type to PNG and apply filters to the image.
$image : An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().
$to : Path to save file (optional).
$quality : Quality of the image (optional).
$filters : Filters to apply to the image which helps in reducing the size.
- imagedestroy( resource $image ) function is used to destroy an image and frees any memory associated with the image.
Conclusion
Here you have learned to build your own custom Captcha code using PHP. This Captcha code is reusable in your further projects as per your requirements. You can also enhance this to make it more challenging and secure.