PHP implementation of CAPTCHA Verification code class instance, CAPTCHA verification code
This paper describes the PHP implementation of the CAPTCHA verification code class, in the PHP program design has a very wide range of applications. Share to everyone for your reference. Here's how:
The Verification code class file is as follows:
<?php/** Captcha Verification Code class * date:2011-02-19* author:fdipzone*/class captcha{//class start private $sname = "; Ction __construct ($sname = ") {//$sname CAPTCHA session name $this->sname = $sname = ="? ' M_captcha ': $sname; }/** generate captcha picture * @param int $length captcha length * @param Array $param parameters * @return IMG */Public Function Create ($length =4, $param =array ()) {Header ("Content-type:image/png"), $authnum = $this->random ($length);//generate CAPTCHA characters. $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 interfering pixels $lnum = isset ($param [' lnum '])? $param [' Lnum ']: 2; Number of interfering 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 of size x_size and y_size. $black = Imagecolorallocate ($im, 238,238,238); Set the 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_ra nd (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 the noise polygon basemap/* 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); Sets the random x-coordinate $y = rand (1, $PH/3); Set the random y-coordinate imagestring ($im, Mt_rand (4,6), $x, $y, substr ($authnum, $i, 1), $font); }/* Add interference pixels */for ($i =0; $i < $pnum; $i + +) {$dist = Imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255 )); Set the dot color Imagesetpixel ($im, Mt_rand (0, $PW), Mt_rand (0, $ph), $dist); }/* Add interference 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 images to a browser or file Imagedestroy ($im) in PNG format; Destroy an image}/** check Verification code * @param String $captcha captcha * @param int $flag verified successfully 0: Do not clear session 1: Clear Session * @return Boolean */P ublic 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 Captcha_session ($this->sname, "); } return true; }else{return false; }}}/* Generates a random number function * @param int $length The string to be randomly generated * @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 Functio N 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?>
The Demo sample program is as follows:
<?php session_start (); Require_once (' Captcha.class.php '); $obj = new Captcha ($sname); # Create Captcha Class object # $sname to save the CAPTCHA session name, leave it blank and leave it as ' M_captcha ' $obj->create ($length, $param); #创建Captcha并输出图片 # $length is captcha length, can be left blank, default is 4 /* $param = Array ( ' width ' = captcha character Widths ') Height ' = ' + captcha character altitude ' pnum ' = number of interference points ' lnum ' and 2 interference lines can be left blank * /$obj->check ($captcha, $flag); # Check that the user entered the correct verification code, true or False # $captcha the user entered the verification code, required # $ Flag can be left blank, default is 1 # 1: Automatically clears captcha session # 0 When validation is successful : block validation does not clear Captcha session, for Ajax check?>
It is believed that this article has some reference value to the study of PHP program design.
Which one gives an example of a zend_captcha to generate a verification code OH-PHP Framework Development
Do not understand why the default verification code Zend so difficult to recognize, people are quickly recognized.
PHP Registration page CAPTCHA Verification Code code is as follows:
You have to judge, judge you to fill in the verification code is not the same as your previous verification code saved
if ($_post["Captcha"]!=$_session["Captcha"])
{
echo "Verification code error";
}
http://www.bkjia.com/PHPjc/882909.html www.bkjia.com true http://www.bkjia.com/PHPjc/882909.html techarticle PHP Implementation of CAPTCHA Verification code class instance, CAPTCHA verification code in this paper, the implementation of PHP CAPTCHA verification code class, in the PHP program design has a very wide range of applications. Share for the big ...