Captcha
The latter code segment of this captcha is to be placed in a separate file that’s invoked by an img tag in the form function. It uses multiple background files but can be modified to randomly generate a background image. The font is picked randomly from 4 different true type fonts and the size/angle is randomized also.
<?php
//Generates a security code within the session file. Used with function captcha_form()
function secure_code() {
$string = str_replace("O","Z",str_shuffle(rand(1,9).chr(rand(65,90)).rand(1,9).chr(rand(65,90)).rand(1,9).chr(rand(65,90))));
if ((time()-$_SESSION['secure_code_life']) > 4||!$_SESSION['secure_code_life']) {
$_SESSION['secure_code'] = $string;
$_SESSION['secure_code_life'] = time();
}
}
//Shows the html which is a image tag and input box, this is to be used within an existing form tag
function captcha_form() {
global $err;
secure_code();
$form = '<img id="secure" src="/img.php?secure=1" /><br />
<input type="text" name="secure" size="6" maxlength="6">'.$err['secure'].' <b>« Enter the code seen in the image above.</b><br>
Click <a href="javascript:redraw_secure()">here to redraw</a> the security code if it is not readable.<br>
If the security code does not redraw <a href="'.$_SERVER['SCRIPT_NAME'].($_SERVER['QUERY_STRING']?'?'.$_SERVER['QUERY_STRING']:'').'">refresh this page</a>.<br>
It should be <b>six</b> alphanumeric characters.
<script type="text/javascript">function redraw_secure() {if (document.getElementById) {if (document.getElementById("secure")) {document.getElementById("secure").src="/img.php?secure=2&r="+Math.random();}}}</script>';
return $form;
}
?>
<?php
//The below code should be located in a separate file called img.php within the root directory (unless you change the path in function captcha_form())
header("Content-type: image/png");
if ($_GET['secure']=='2') secure_code(); //regenerate the code, used in redraw js function in captcha_form()
//the glob functiton here is just grabbing a collection of background images designed to randomize/obscure the security code a little
//this background could just as well be generated at random during runtime though
$background = glob(ini_get('doc_root').'images/security*.jpg');
$background = $background[rand(0,count($background)-1)];
$im = imagecreatefromjpeg($background);
$color = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
$px = rand(imagesx($im)-250,imagesx($im)-80);
$py = rand(imagesy($im)-10,imagesy($im)-20);
//Randomizing the fonts used and the sizes, these are true type fonts and PHP is located on a windows machine in this example
$fonts = array($_SERVER['windir'].'/fonts/impact.ttf',
$_SERVER['windir'].'/fonts/ariblk.ttf',
$_SERVER['windir'].'/fonts/breakaway.ttf',
$_SERVER['windir'].'/fonts/bricklet.ttf');
$font = $fonts[rand(0,count($fonts)-1)];
$fontsize = rand(13,20);
$angle = rand(-15,15);
//Write the security code to the image and output the image
imagettftext($im,$fontsize,$angle,$px,$py,$color,$font,$_SESSION['secure_code']);
imagepng($im);
imagedestroy($im);
?>
