
Creating a CAPTCHA class in PHP can help you add a level of security to your web forms by ensuring that the submitted data comes from a human, rather than an automated system.
<?php class SimpleCaptcha { private $code; private $width; private $height; public function __construct($width = 150, $height = 40) { $this->width = $width; $this->height = $height; $this->code = $this->generateCode(); } // Generate a random code for CAPTCHA private function generateCode($length = 6) { $characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; // Avoid ambiguous characters $code = ''; for ($i = 0; $i < $length; $i++) { $code .= $characters[rand(0, strlen($characters) - 1)]; } return $code; } // Render CAPTCHA image public function render() { // Create an image $image = imagecreatetruecolor($this->width, $this->height); // Allocate colors $bgColor = imagecolorallocate($image, 255, 255, 255); // white $textColor = imagecolorallocate($image, 0, 0, 0); // black $lineColor = imagecolorallocate($image, 64, 64, 64); // grey $pixelColor = imagecolorallocate($image, 0, 0, 255); // blue // Fill the background imagefilledrectangle($image, 0, 0, $this->width, $this->height, $bgColor); // Add some random lines for obfuscation for ($i = 0; $i < 5; $i++) { imageline($image, 0, rand() % $this->height, $this->width, rand() % $this->height, $lineColor); } // Scatter some random dots for ($i = 0; $i < 500; $i++) { imagesetpixel($image, rand() % $this->width, rand() % $this->height, $pixelColor); } // Add the text $fontFile = __DIR__ . '/arial.ttf'; // Path to a TTF font file $fontSize = 20; $textBox = imagettfbbox($fontSize, 0, $fontFile, $this->code); $textWidth = $textBox[2] - $textBox[0]; $textHeight = $textBox[7] - $textBox[1]; $x = ($this->width - $textWidth) / 2; // Center the text horizontally $y = ($this->height - $textHeight) / 2; // Center vertically, adjust as needed imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $this->code); // Output the image header("Content-type: image/png"); imagepng($image); imagedestroy($image); } // Get the current CAPTCHA code public function getCode() { return $this->code; } } // Usage example $captcha = new SimpleCaptcha(); session_start(); $_SESSION['captcha'] = $captcha->getCode(); $captcha->render();
Explanation:
- Class Initialization:
- The class
SimpleCaptcha
is initialized with a default width and height for the CAPTCHA image. - A random code is generated when an object of the class is instantiated.
- The class
- Code Generation:
- The
generateCode()
method creates a random sequence of characters excluding ambiguous ones like ‘O’ and ‘0’.
- The
- Image Rendering:
imagecreatetruecolor()
is used to create a blank image with the specified dimensions.- Colors are allocated for the background, text, random lines, and pixels.
- The background is filled with a light color (white in this case).
- Random lines and pixels are added to make the CAPTCHA more difficult for bots to read.
- Text Rendering:
- The code is drawn onto the image using
imagettftext()
for a better font rendering. - Text is centered on the image for aesthetic appeal.
- The code is drawn onto the image using
- Output:
- The image is output as a PNG file directly to the browser.
- Session Storage:
- The generated code is saved in a session to verify against user input later.
Considerations:
- Ensure that the TTF font file (
arial.ttf
in this case) is available and correct. - Depending on your environment, PHP GD library should be installed and configured to handle image creation and manipulation.
- You should adjust the fonts, colors, and complexity to balance readability for humans and difficulty for automated systems.