PHP Simple Verification Code class: letter + Digital Font random distortion
Now a variety of verification code, personally think that the verification code is for users to see, so simple is good. What interference codes, tilt, complex backgrounds are a torment to the user.
Look at some of the more large experience better site verification code is relatively simple, no complex background, no interference code, no background.
There is no place to write bad welcome everyone to criticize, pointing.
Length) {Mt_srand (double) microtime () *1000000), $randnum =mt_rand (50,90), if (!in_array ($randnum, Array ( 58,59,60,61,62,63,64,73,79)) {$authnum = $authnum. chr ($randnum); $i + +;}} Session_Start (); $time =time (); $checkcode =md5 (MD5 ($authnum. ' Initphpyzmsy ' $time)); $key = $time. ', '. $checkcode. ', '. authnum;$_session[' initphp_code ' = $key; return $authnum;} /** * Get Verification code Picture * @param $width width * @param $height high * @param $warping font random twist switch 0 = off, 1 = on * @return string */public function g Etcode ($width =140, $height =40, $warping =0) {$this->width= $width; $this->height= $height; $this->warping=$ Warping;if ($this->type<2&& function_exists (' imagecreate ') && function_exists (' Imagecolorset ') && function_exists (' imagecopyresized ') && function_exists (' imagecolorallocate ') && function_exists (' Imagechar ') && function_exists (' Imagecolorsforindex ') &&function_ Exists (' Imageline ') && function_exists (' imagecreatefromstring ') && (function_exists (' imagegif ') | | Function_exists (' imagepng ') | | Function_exists (' imagejpeg ')) {$this->image ();}} /** * Generate picture Verification code * @return string */public function image () {$this->im=imagecreate ($this->width, $this->height);// Set the picture background size imagecolorallocate ($this->im, 243, 251, 254);//Set Background $this->color=imagecolorallocate ($this->im, Mt_ Rand (1,120), Mt_rand (1,120), Mt_rand (1,120));//Captcha font random color $ttfpath = dirname (__file__). '/font/';//font Directory $dirs = Opendir ($ttfPath), $seccodettf = Array (), while ($entry = Readdir ($dirs)) {if ($entry! = '. ' && Amp $entry! = ': ' && in_array (Strtolower (Addslashes (substr (STRRCHR ($entry, '. '), 1,))), Array (' TTF ', ' TTC ')) {$seccodettf [] = $ttfPath. $entry;}} $ttf = $seccodettf [Array_rand ($seccodettf)];//a random font $size = $this->type? $this->WIDTH/7: $this->width/6;//font size imagettftext ($this->im, $size, 0, ten, $size *1.2, $this->color, $ttf, $this->get_random_val ());//Set the CAPTCHA character if ($this->warping) {//Random twist $this->setwarping ();} if (function_existS ("Imagepng")) {header ("Content-type:image/png"), $code =imagepng ($this->im);} ElseIf (function_exists ("imagejpeg")) {header ("Content-type:image/jpeg"), $code =imagejpeg ($this->im);} ElseIf (function_exists ("Imagegif")) {header ("Content-type:image/gif"), $code =imagegif ($this->im);} Imagedestroy ($this->im); return $code;} /** * Check Verification code * @param $code * @return bool */public function Checkcode ($code) {session_start (); $secode =explode (', ', $_sessi on[' Initphp_code '); $time =time ();//Check whether the time expires if ($secode [0]> $time | | $time-$secode [0]> $this->time) {return false;} Verify that the key is double MD5 after the IF ($secode [1]<>md5 ($code. ' Initphpyzmsy '. $secode [0])) {return false;} Check that the verification code string is consistent if ($code | | $code <> $secode [2]) {return false;} return true;} /* Random twist */public function setwarping () {$rgb =array (); $direct =rand (0,1); $width = Imagesx ($this->im); $height = Imagesy ($this->im); $level = $width/20;for ($j = 0; $j < $height; $j + +) {for ($i = 0; $i < $width; $i + +) {$rgb [$i] = Imagecolora T ($this->im, $i, $j);} for ($i = 0; $i < $width; $i + +) {$r = sin ($j/$height * 2 * M_PI-M_PI * 0.5) * ($direct? $level:-$level); IMAGESETPI Xel ($this->im, $i + $r, $j, $rgb [$i]);}}}? >
Effect: