Captcha: How to create a CAPTCHA
Step 1:
Please first create a folder named /captcha/. In that folder, please create a file named captcha.php.Source code of the captcha.php file:
<?php
session_start();
unset($_SESSION['captcha_spam']);
function randomString($len) {
srand(date("s"));
//The $possible string contains all characters, which should be used
$possible="ABCDEFGHJKLMNPRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789";
$str="";
while(strlen($str)<$len) {
$str.=substr($possible,(rand()%(strlen($possible))),1);
}
return($str);
}
$text = randomString(5); //The number defines the amount of digits
$_SESSION['captcha_spam'] = $text;
header('Content-type: image/png');
$img = ImageCreateFromPNG('captcha.PNG'); //Background image
$color = ImageColorAllocate($img, 0, 0, 0); //Color
$ttf = "./XFILES.TTF"; //Font
$ttfsize = 25; //Font size
$angle = rand(0,5);
$t_x = rand(5,30);
$t_y = 35;
imagettftext($img, $ttfsize, $angle, $t_x, $t_y, $color, $ttf, $text);
imagepng($img);
imagedestroy($img);
?>
Description of variables:
$img = ImageCreateFromPNG('captcha.PNG'); - "captcha.PNG" is the filename of the background image, on which the characters will be depicted. You can either create an own image with an image manipulation program or you use this. Copy the desired image into the "captcha" subfolder.
$color = ImageColorAllocate($img, 0, 0, 0); - Here you can define the color, default is black (0,0,0), white is 255,255,255 for instance.
$ttf = "XFILES.TTF"; - This is the font which is used for the generated characters to be displayed. Copy the desired font into the "captcha" subfolder. The font which we use can be found here.
$ttfsize = 25; - Here you can define the font size of the generated font.
Search in support and FAQ