PHP簡單驗證碼類:字母+數字 字型隨機扭曲
現在各式各樣的驗證碼,個人認為驗證碼是讓使用者看的,所以簡單就好。什麼幹擾碼,傾斜,複雜的背景 都是對使用者的一種折磨。
看了一些比較大的體驗比較好的網站驗證碼都較為簡單,沒有複雜的背景,沒有幹擾碼,沒有背景。
有什麼地方寫的不好的歡迎大家批評,指點。
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;}/** * 擷取驗證碼圖片 * @param $width 寬 * @param $height 高 * @param $warping 字型隨機扭曲開關 0=關,1=開 * @return string */public function getcode($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();}}/** * 產生圖片驗證碼 * @return string */public function image(){$this->im=imagecreate($this->width, $this->height);//設定圖片背景大小imagecolorallocate($this->im, 243, 251, 254);// 設定背景 $this->color=imagecolorallocate($this->im, mt_rand(1,120), mt_rand(1,120), mt_rand(1,120));// 驗證碼字型隨機顏色$ttfPath = dirname(__FILE__) . '/font/';//字型目錄$dirs = opendir($ttfPath);$seccodettf = array();while($entry = readdir($dirs)) {if($entry != '.' && $entry != '..' && in_array(strtolower(addslashes(strtolower(substr(strrchr($entry, '.'), 1, 10)))), array('ttf', 'ttc'))) {$seccodettf[] = $ttfPath.$entry;}}$ttf = $seccodettf[array_rand($seccodettf)];//隨機一種字型$size = $this->type ? $this->width / 7 : $this->width / 6;//字型大小imagettftext($this->im,$size, 0, 10, $size*1.2, $this->color, $ttf, $this->get_random_val());//設定驗證碼字元if($this->warping){//隨機扭曲$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;}/** * 檢查驗證碼 * @param $code * @return bool */public function checkCode($code) {session_start();$secode=explode(',', $_SESSION['initphp_code']);$time=time();//檢查時間是否到期if($secode[0]>$time||$time-$secode[0]>$this->time){return false;}//驗證碼密鑰 雙md5 後是否一致if($secode[1]<>md5(md5($code.'initphpYzmsy'.$secode[0]))){return false;}//檢查驗證碼字串是否一致if($code||$code<>$secode[2]){return false;}return true;}/*隨機扭曲*/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] = imagecolorat($this->im, $i , $j);}for($i = 0;$i < $width;$i++) {$r = sin($j / $height * 2 * M_PI - M_PI * 0.5) * ($direct ? $level : -$level); imagesetpixel($this->im, $i + $r , $j , $rgb[$i]);}}}}?>
效果: