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(); SimpleCaptcha is initialized with a default width and height for the CAPTCHA image.generateCode() method creates a random sequence of characters excluding ambiguous ones like ‘O’ and ‘0’.imagecreatetruecolor() is used to create a blank image with the specified dimensions.imagettftext() for a better font rendering.arial.ttf in this case) is available and correct.This guide provides a detailed walkthrough of installing and configuring an Apache web server and…
WordPress development has evolved significantly, and modern tooling plays a crucial role in creating efficient…
I. Project Overview The goal is to automate the process of notifying search engines (like…
1. Database Structure (MySQL) We'll need a database table to store information about our website's…
This explanation aims to provide a solid foundation for understanding the process and implementing your…
Okay, here's a comprehensive guide on building a real-time website chat script using PHP, HTML,…