php驗(yàn)證碼生成器
現(xiàn)在很多網(wǎng)站都有實(shí)現(xiàn)用戶集。然而為了防止機(jī)器人的網(wǎng)絡(luò)攻擊。限制登陸或者注冊是有必要的。
在注冊和登陸時(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:請輸入正確驗(yàn)證碼.'; } } } } } ?>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于wordpress主題制作的具體實(shí)現(xiàn)步驟
本篇文章是對wordpress主題制作的具體實(shí)現(xiàn)步驟進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05php使用pdo連接mssql server數(shù)據(jù)庫實(shí)例
這篇文章主要介紹了php使用pdo連接mssql server數(shù)據(jù)庫的方法,以實(shí)例形式分析了php使用pdo連接mssql server數(shù)據(jù)庫的技巧,非常簡單實(shí)用,需要的朋友可以參考下2014-12-12JS(jQuery)實(shí)現(xiàn)聊天接收到消息語言自動(dòng)提醒功能詳解【提示“您有新的消息請注意查收”】
這篇文章主要介紹了JS(jQuery)實(shí)現(xiàn)聊天接收到消息語言自動(dòng)提醒功能,結(jié)合實(shí)例形式詳細(xì)分析了javascript結(jié)合ajax后臺(tái)交互實(shí)現(xiàn)信息語音提示功能相關(guān)原理與操作技巧,需要的朋友可以參考下2019-04-04PHP中使用Imagick讀取pdf并生成png縮略圖實(shí)例
這篇文章主要介紹了PHP中使用Imagick讀取pdf并生成png縮略圖實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-01-01