您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“php驗(yàn)證碼生成器的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“php驗(yàn)證碼生成器的示例分析”這篇文章吧。
現(xiàn)在很多網(wǎng)站都有實(shí)現(xiàn)用戶集。然而為了防止機(jī)器人的網(wǎng)絡(luò)攻擊。限制登陸或者注冊(cè)是有必要的。
在注冊(cè)和登陸時(shí)強(qiáng)制要求輸入一個(gè)機(jī)器難以識(shí)別的字符串集是一個(gè)不錯(cuò)的選擇。雖然不能解決根本問題,但至少可以增加他們的成本。
利用PHP生成驗(yàn)證碼需要用到GD2庫。GD2庫引用方法網(wǎng)絡(luò)上有很多,不同操作系統(tǒng)導(dǎo)入方式也不同。
這段代碼運(yùn)行在WINDOS服務(wù)器平臺(tái)
<?php $iC = new idCode(5,60,30); $iC->createPNG(); class idCode{ private $words = array('a','b', 'c','d','e','f','g','h','i','j','k','l', 'm','n','o','p','q','r','s','t','u','v', 'w','x','y','z','A','B','C','D','E','F', 'G','H','I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X','Y','Z', '0','1','2','3','4','5','6','7','8','9'); private $fonts; private $count;//驗(yàn)證碼字符數(shù) private $height; private $width; private $path = '..\myfolder\fonts'; private $keys; //構(gòu)造函數(shù) public function __construct($count,$width,$height){ $this->count = $count; $this->getFonts(); $this->height = $height; $this->width = $width; } private function getFonts(){ $dir = dir($this->path); while(false !== ($file = $dir->read())){ if($file != '.' && $file != '..'){ $this->fonts[count($this->fonts)] = basename($file); } } $dir->close(); } private function createKeys(){ for($i = 0;$i < $this->count;$i++){ $this->keys[$i]['char'] = $this->words[rand(0,count($this->words)-1)]; //使用字體路徑標(biāo)識(shí) $this->keys[$i]['filename'] = $this->path.'\\'.$this->fonts[rand(0,count($this->fonts)-1)]; } } public function createPNG(){ $this->createKeys(); //創(chuàng)建畫布以及顏色塊兒 $bg = imagecreatetruecolor($this->width + 10*2,$this->height + 3*2);//兩邊留10px空白,上下3px $grey = imagecolorallocate($bg,155,155,155); $blue = imagecolorallocate($bg,0x00,0x00,0xff); //填充背景 imagefill($bg,0,0,$grey); //添加字符 $pwidth = $this->width/$this->count; $x;$y; for($i = 0;$i < $this->count;$i++){ $rotation = rand(-40,40);//偏轉(zhuǎn)角度±40° $fontsize = 33; $width_txt; $height_txt; do{ $fontsize--; $bbox = imagettfbbox($fontsize,$rotation,$this->keys[$i]['filename'],$this->keys[$i]['char']); $width_txt = $bbox[2] - $bbox[0];//x 0 2 4 6,y1 3 5 7;左下,右下,右上,左上 $height_txt = $bbox[7] - $bbox[1]; }while($fontsize > 8 && ($height_txt > $this->height || $width_txt > $pwidth)); $fontcolor = imagecolorallocate($bg,rand(0,255),rand(0,255),rand(0,255)); $x = 8 + $pwidth*$i + $pwidth/2 - $width_txt/2;//x坐標(biāo)基本位置 $y = $this->height/2 - $height_txt/2; imagettftext($bg,$fontsize,$rotation,$x,$y,$fontcolor,$this->keys[$i]['filename'],$this->keys[$i]['char']); } //繪制干擾線 //根據(jù)字體酌情增加干擾線 imageline($bg,0,15,40,10,$blue); //圖像輸出頭文件 header('Content-type:image/png'); //輸出png圖像 imagepng($bg); //清除緩存資源 imagedestroy($bg); } public function checkKeys($input){ if(count($input)!=$this->count){ return 'ERROR:長度不正確.'; }else{ for($i=0;$i < $this->count;$i++){ //0 o O I l 1 校準(zhǔn),根據(jù)所選擇的字體確定是否需要手動(dòng)校準(zhǔn) if($input[$i] != $this->keys[$i]['char']){ return 'SUCCESS.'; }else{ return 'ERROR:請(qǐng)輸入正確驗(yàn)證碼.'; } } } } } ?>
以上是“php驗(yàn)證碼生成器的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。