標籤:驗證碼 php
驗證碼這樣的功能可以說是無處不在了,接下來使用php來實現驗證碼這樣的功能,這裡我是將驗證碼實現抽取到一個類中獨立開來,那麼後面如果再使用到驗證碼功能,直接引入該類檔案並建立該類的執行個體,就可以使用驗證碼了,代碼如下:
驗證碼類檔案vcode.class.php
<?php/** *驗證碼類 */ class Vcode{private $width;//圖片寬度private $height;//圖片高度private $num;//驗證碼個數private $img;//圖片資源private $code;//驗證碼private $pointNum;//幹擾點個數private $lineNum;//幹擾線個數private $fontFile;//字型檔//建構函式初始化相關資料function __construct($width=85,$height=34,$num=4){$this->width=$width;$this->height=$height;$this->num=$num;$this->code=$this->createCode();$this->pointNum=100;$this->lineNum=10;$this->fontFile="STLITI.TTF";}/** *用於設定成員屬性 *@param string $key 成員屬性名 *@param mixed $value 成員屬性值 *@return object 返回自己對象$this,可用於連貫操作 */public function set($key,$val){//get_class_vars() 擷取類中的屬性群組成的數組//get_class() 返回對象的類名if(array_key_exists($key,get_class_vars(get_class($this)))){$this->setOption($key,$val);}return $this;}//設定參數private function setOption($key,$value){$this->$key=$value;}//擷取驗證碼public function getCode(){return $this->code;}//輸出映像public function outImg(){//建立映像$this->createImage();//畫驗證碼$this->drawCode();//畫幹擾元素$this->drawDisturbColor();//輸出映像$this->printImg();}//畫驗證碼private function drawCode(){$this->fontFile="./font/".$this->fontFile;for($i=0;$i<$this->num;$i++){//設定隨機顏色$randColor=imagecolorallocate($this->img,rand(0,128),rand(0,128),rand(0,128));//字型大小$fontSize=rand(20,23);//字型水平位置$x=($this->width/$this->num)*$i;//水平方向的位置$y=rand($fontSize,imagefontheight($fontSize)+3);//畫字型imagettftext($this->img,$fontSize,0,$x,$y,$randColor,$this->fontFile,$this->code{$i});}}//畫幹擾元素private function drawDisturbColor(){//畫幹擾點for($i=0;$i<$this->pointNum;$i++){//設定隨機顏色$randColor=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255));//畫點imagesetpixel($this->img,rand(1,$this->width-2),rand(1,$this->height-2),$randColor);}//畫幹擾線for($i=0;$i<$this->lineNum;$i++){//設定隨機顏色$randColor=imagecolorallocate($this->img,rand(0,200),rand(0,200),rand(0,200));//畫線imageline($this->img,rand(1,$this->width-2),rand(1,$this->height-2),rand(1,$this->height-2),rand(1,$this->width-2),$randColor);}}//建立映像private function createImage(){//建立一個真彩色映像$this->img=imagecreatetruecolor($this->width,$this->height);//設定背景色$bgColor=imagecolorallocate($this->img,rand(200,255),rand(200,255),rand(200,255));//填充背景色imagefill($this->img,0,0,$bgColor);//設定邊框顏色$borderColor=imagecolorallocate($this->img,0,0,0);//畫一個邊框imagerectangle($this->img,0,0,$this->width-1,$this->height-1,$borderColor);}//輸出映像private function printImg(){if(imagetypes() & IMG_PNG){//針對pngheader("Content-Type:image/png");imagepng($this->img);}else if(imagetypes() & IMG_JPG){//針對jpgheader("Content-Type:image/jpeg");imagejpeg($this->img,null,100);}else if(imagetypes() & IMG_GIF){//針對Gifheader("Content-Type:image/gif");imagegif($this->img);}else if(imagetypes() & IMG_WBMP){// 針對 WBMPheader('Content-Type: image/vnd.wap.wbmp');imagewbmp($this->img);}else{die('No image support in this PHP server');}}//建立驗證碼private function createCode(){//預設字串$codes="123456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY";//產生驗證碼$code="";for($i=0;$i<$this->num;$i++){$code.=$codes{rand(0,strlen($codes)-1)};}return $code;}//解構函式用於銷毀映像資源function __destruct(){imagedestroy($this->img);} }
這裡我使用的畫字型的函數是imagettftext(),因為這個函數可以自訂字型樣式,從代碼中也能看出來,傳入的參數有個字型檔屬性,如果不喜歡用這個函數可以使用imagestring()函數也行,只不過個人覺得這個函數的預設字型大小,也不好看。還是自訂字型看著舒服點。接下來是調用驗證碼類
<?php//開啟Sessionsession_start();//引入驗證碼類include("vcode.class.php");//建立驗證碼類$vcode=new Vcode();//將擷取的驗證碼存入到session中$_SESSION['code']=$vcode->getCode();//$vcode->set("pointNum",10);//自訂幹擾點個數//$vcode->set("lineNum",10);//自訂幹擾線個數//$vcode->set("fontFile","wawa.ttf");//自訂字型檔//輸出映像$vcode->outImg();
代碼到這裡驗證碼就實現了,直接調用該檔案也能看到驗證碼,下面使用一個簡單的登入表單使用該驗證碼
<?php//開啟Sessionsession_start();//判斷是否提交if(isset($_POST['dosubmit'])){//擷取session中的驗證碼並轉為小寫$sessionCode=strtolower($_SESSION['code']);//擷取輸入的驗證碼$code=strtolower($_POST['code']);//判斷是否相等if($sessionCode==$code){echo "<script type='text/javascript'>alert('驗證碼正確!');</script>";}else{echo "<script type='text/javascript'>alert('驗證碼錯誤!');</script>";}}?><!DOCTYPE html><html><head><title></title><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><style type="text/css">*{margin:0px;padding:0px;}ul{width:400px;list-style:none;margin:50px auto;}li{padding:12px;position:relative;}label{width:80px;display:inline-block;float:left;line-height:30px;}input[type='text'],input[type='password']{height:30px;}img{margin-left:10px;}input[type="submit"]{margin-left:80px;padding:5px 10px;}</style></head><body><form action="login.php" method="post"><ul><li><label>使用者名稱:</label><input type="text" name="username"/></li><li><label>密碼:</label><input type="password" name="password"/></li><li><label>驗證碼:</label><input type="text" name="code" size="4" style="float:left"/><img src="image_002.php" onclick="this.src='image_002.php?Math.random()'"/></li><li><input type="submit" value="登入" name="dosubmit"/></li></ul></form></body></html>
實現的效果:
Php實現驗證碼功能