CAPTCHAs are used to prevent automated software from performing actions which degrade the quality of a service of a given system, whether due to abuse or resource expenditure. Creates a CAPTCHA image in PHP, which displays 5 numbers. The code is stored in a session variable ... then, the user must enter the code correctly. Can be used on registration forms, login forms, etc.
<?php
session_start ();
header ( 'Content-type: image/png' );
$im = imagecreate ( 180 , 70 );
$lcolor1 = imagecolorallocate ( $im , 0 , 0 , 0 );
$lcolor2 = imagecolorallocate ( $im , rand ( 0 , 70 ), rand ( 0 , 70 ), rand ( 0 , 70 ));
$bgcolor2 = imagecolorallocate ( $im , rand ( 200 , 255 ), rand ( 200 , 255 ), rand ( 200 , 255 ));
$_SESSION [ 'code' ] = str_replace ( '7' , '1' , rand ( 10000 , 99999 ));
$id = $_SESSION [ 'code' ];
imagefill ( $im , 0 , 0 , $bgcolor2 );
$end1 = rand ( 0 , 100 );
imagefilledrectangle ( $im , 0 , 0 , $end1 , 70 , ImageColorAllocate ( $im , rand ( 235 , 255 ), rand ( 235 , 255 ), rand ( 235 , 255 )));
$end2 = $end1 + rand ( 0 , 100 );
imagefilledrectangle ( $im , $end1 , 0 , $end2 , 70 , ImageColorAllocate ( $im , rand ( 235 , 255 ), rand ( 235 , 255 ), rand ( 235 , 255 )));
$end3 = $end2 + rand ( 0 , 100 );
imagefilledrectangle ( $im , $end2 , 0 , $end3 , 70 , ImageColorAllocate ( $im , rand ( 235 , 255 ), rand ( 235 , 255 ), rand ( 235 , 255 )));
imagefilledrectangle ( $im , $end3 , 0 , 180 , 70 , ImageColorAllocate ( $im , rand ( 235 , 255 ), rand ( 235 , 255 ), rand ( 235 , 255 )));
$y1 = rand ( 25 , 50 );
ImageTTFText ( $im , rand ( 17 , 20 ), rand (- 30 , 30 ), rand ( 10 , 20 ), $y1 , $lcolor1 , 'fonts/verdana.ttf' , substr ( $id , 0 , 1 ));
$y2 = rand ( 25 , 50 );
ImageTTFText ( $im , rand ( 17 , 20 ), rand ( 0 , 30 ), rand ( 45 , 55 ), $y2 , $lcolor2 , 'fonts/trebuc.ttf' , substr ( $id , 1 , 1 ));
$y3 = rand ( 25 , 50 );
ImageTTFText ( $im , rand ( 17 , 25 ), rand (- 20 , 0 ), rand ( 80 , 90 ), $y3 , $lcolor1 , 'fonts/trebuc.ttf' , substr ( $id , 2 , 1 ));
$y4 = rand ( 25 , 50 );
ImageTTFText ( $im , rand ( 16 , 25 ), rand ( 0 , 30 ), rand ( 115 , 125 ), $y4 , $lcolor1 , 'fonts/verdana.ttf' , substr ( $id , 3 , 1 ));
$y5 = rand ( 25 , 50 );
ImageTTFText ( $im , rand ( 18 , 20 ), rand (- 25 , 0 ), rand ( 150 , 160 ), $y5 , $lcolor2 , 'fonts/trebuc.ttf' , substr ( $id , 4 , 1 ));
$x = rand ( 0 , 50 );
$y = rand ( 0 , 50 );
imagepng ( $im );
?>