This article describes the PHP implementation of the CAPTCHA authentication code class, in the PHP program design has a very wide range of applications. Share to everyone for your reference. The specific methods are as follows:
<?php/** Captcha Authentication Code class * DATE:2011-02-19 * Author:fdipzone/class captcha{//class start private $sname = '; Public function __construct ($sname = ") {//$sname CAPTCHA session name $this->sname = $sname = = '?
' M_captcha ': $sname; /** generate a CAPTCHA picture * @param int $length Verification Code length * @param Array $param parameters * @return IMG/Public Function Create ($length =4
, $param =array ()) {Header ("content-type:image/png"); $authnum = $this->random ($length);
Generates a CAPTCHA character. $width = Isset ($param [' width '])? $param [' width ']: 13; Text width $height = isset ($param [' height '])? $param [' Height ']: 18; Text Height $pnum = isset ($param [' pnum '])? $param [' Pnum ']: 100; Number of jamming pixels $lnum = isset ($param [' lnum '])? $param [' Lnum ']: 2; Number of interference lines $this->captcha_session ($this->sname, $authnum);
Writes a random number to the session $PW = $width * $length +10;
$PH = $height +6; $im = Imagecreate ($PW, $ph);
Imagecreate () Creates a new image with a blank image size of x_size and y_size. $black = Imagecolorallocate ($im, 238,238,238); Set background color $values = arrAy (mt_rand (0, $PW), Mt_rand (0, $ph), Mt_rand (0, $PW), Mt_rand (0, $ph), Mt_rand (0, $PW), Mt_rand (0, $ph), Mt_rand (0, $PW)
, Mt_rand (0, $ph), Mt_rand (0, $PW), Mt_rand (0, $ph), Mt_rand (0, $PW), Mt_rand (0, $ph)); Imagefilledpolygon ($im, $values, 6, Imagecolorallocate ($im, Mt_rand (170,255), Mt_rand (200,255), Mt_rand (210,255))); Set interference Polygon Bottom chart * * Text/for ($i = 0; $i < strlen ($authnum); $i + +) {$font = Imagecolorallocate ($im, Mt_rand (0,50), Mt_ Rand (0,150), Mt_rand (0,200));/Set text color $x = $i/$length * $PW + rand (1, 6); Set the random x-coordinate $y = rand (1, $PH/3);
Set random y-coordinate imagestring ($im, Mt_rand (4,6), $x, $y, substr ($authnum, $i, 1), $font); }/* Add the jamming pixel */for ($i =0; $i < $pnum; $i + +) {$dist = Imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0, 255));
Set the miscellaneous point color Imagesetpixel ($im, Mt_rand (0, $PW), Mt_rand (0, $ph), $dist); /* Add the Jamming line * * for ($i =0; $i < $lnum; $i + +) {$dist = Imagecolorallocate ($im, Mt_rand (50,255), Mt_rand (150,255), Mt_rand (200,255)); Set Line Color Imageline ($Im,mt_rand (0, $PW), Mt_rand (0, $ph), Mt_rand (0, $PW), Mt_rand (0, $ph), $dist); } imagepng ($im); Output the image to the browser or file Imagedestroy ($im) in PNG format;
Destroy an image}/** check Verification code * @param String $captcha Verification Code * @param int $flag validation successful 0: Do not clear session 1: Clear Session * @return Boolean
*/Public function check ($captcha, $flag =1) {if (empty ($captcha)) {return false; }else{if (strtoupper ($captcha) = = $this->captcha_session ($this->sname) {//Detect Verification code if ($flag ==1) {$this->CAPTC
Ha_session ($this->sname, ");
return true;
}else{return false; }}/* Generate random number function * @param int $length need randomly generated string numbers * @return String */Private function random ($length) {$hash = '
';
$chars = ' ABCDEFGHIJKLMNPQRSTUVWXYZ23456789 ';
$max = strlen ($chars)-1;
for ($i = 0; $i < $length; $i + +) {$hash. = $chars [Mt_rand (0, $max)];
return $hash; /** Validation code Session processing method * @param string $name captcha Session name * @param string $value * @return string/private function Captcha_session ($name, $value =null{if (Isset ($value)) {if ($value!== ') {$_session[$name] = $value;
}else{unset ($_session[$name]);
}else{return Isset ($_session[$name])? $_session[$name]: ';
}}//class end?>
I believe this article describes the learning of PHP program design for a certain reference value.