国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

Php實現驗證碼功能

2020-03-22 17:56:00
字體:
來源:轉載
供稿:網友
  • 驗證碼這樣的功能可以說是無處不在了,接下來使用php來實現驗證碼這樣的功能,這里我是將驗證碼實現抽取到一個類中獨立開來,那么后面如果再使用到驗證碼功能,直接引入該類文件并創建該類的實例,就可以使用驗證碼了,代碼如下:

    驗證碼類文件vcode.html' target='_blank'>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){			//針對png			header("Content-Type:image/png");			imagepng($this->img);		}else if(imagetypes() & IMG_JPG){			//針對jpg			header("Content-Type:image/jpeg");			imagejpeg($this->img,null,100);		}else if(imagetypes() & IMG_GIF){			//針對Gif			header("Content-Type:image/gif");			imagegif($this->img);		}else if(imagetypes() & IMG_WBMP){			// 針對 WBMP			header('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	//開啟Session	session_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	//開啟Session	session_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編程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岑溪市| 安吉县| 洛宁县| 芜湖县| 安吉县| 仙桃市| 云林县| 静安区| 祁门县| 久治县| 阳江市| 铜梁县| 咸宁市| 方山县| 石家庄市| 云南省| 邢台市| 遂平县| 阿荣旗| 信丰县| 阳城县| 通许县| 磐安县| 玛纳斯县| 泾源县| 宁武县| 类乌齐县| 永登县| 边坝县| 锦州市| 如东县| 萨迦县| 宜昌市| 青龙| 绥滨县| 浪卡子县| 水富县| 申扎县| 定西市| 兴业县| 乌什县|