1. 程式人生 > >驗證碼驗證實例

驗證碼驗證實例

math get str aps nbsp -a close capi tar

技術分享
  1 <?PHP
  2 /**
  3  * 驗證碼類
  4  */
  5  class VerificationCode
  6  {
  7      private $code;                //驗證碼內容
  8     private $codelen=4;            //驗證碼長度
  9     private $width=130;            //圖片寬度
 10     private $height=50;            //圖片高度
 11     private $img;                //圖形資源句柄
 12     private
$font="impact.ttf";//指定的字體 13 private $fontsize=25; //指定字體大小 14 private $type=2; //驗證碼內容模式 15 private $dotNum=800; //噪點數 16 private $lineNum=20; //幹擾線數 17 ////////////////////生成驗證碼內容//////////////////// 18 private function randomCode() 19 { 20 $number="0123456789"; 21 $lowercaseLetters
="qwertyuiopasdfghjklzxcvbnm"; 22 $capitalLetter="QWERTYUIOPASDFGHJKLZXCVBNM"; 23 switch ($this->type) //根據type設置驗證碼樣本str 24 { 25 case 0://純數字 26 $str=$number; 27 break; 28 case 1://純字母 29 $str=$lowercaseLetters
.$capitalLetter; 30 break; 31 case 2://數字+小寫字母 32 $str=$number.$lowercaseLetters; 33 break; 34 case 3://數字+字母 35 $str=$number.$lowercaseLetters.$capitalLetter; 36 break; 37 } 38 $code=""; 39 for ($i=0;$i<$this->codelen;$i++) 40 $code.=$str[rand(0,strlen($str)-1)]; 41 $this->code=$code; 42 } 43 ////////////////////獲取隨機顏色(較深)//////////////////// 44 private function randomColor() 45 { 46 return imagecolorallocate($this->img,rand(0,200),rand(0,200),rand(0,200)); 47 } 48 ////////////////////繪制畫布//////////////////// 49 private function createBg() 50 { 51 /*resource imagecreatetruecolor(int $width,int $height) 52 *新建一個大小為width*height的真彩色圖像,返回圖像標識符 53 */ 54 $this->img=imagecreatetruecolor($this->width,$this->height); 55 /*int imagecolorallocate(resource $image,int $red,int $green,int $blue) 56 *返回一個標識符,代表了由給定的 RGB 成分組成的顏色。失敗返回-1. 57 *red,green 和 blue 分別是所需要的顏色的紅,綠,藍成分。 58 這些參數是 0 到 255 的整數或者十六進制的 0x00 到 0xFF。 59 */ 60 $bg=imagecolorallocate($this->img,rand(157,255),rand(157,255),rand(157,255)); 61 /*bool imagefill(resource $image,int $x,int $y,int $color) 62 *在image圖像的坐標x,y處(圖像左上角為 0,0)用color顏色執行區域填充(即與x, y點顏色相同且相鄰的點都會被填充)。 63 */ 64 imagefill($this->img,0,0,$bg); 65 } 66 ////////////////////繪制噪點和幹擾線////////////////////noise 67 private function createNoise() 68 { 69 /*bool imagesetpixel(resource $image,int $x,int $y,int $color) 70 *在 image 圖像中用 color 顏色在 x,y 坐標(圖像左上角為 0,0)上畫一個點。 71 */ 72 for ($i=0;$i<$this->dotNum;$i++) 73 imagesetpixel($this->img,rand(0,$this->width),rand(0,$this->height),$this->randomColor()); 74 /*bool imageline(resource $image,int $x1,int $y1,int $x2,int $y2,int $color) 75 *用 color 顏色在圖像 image 中從坐標 x1,y1 到 x2,y2(圖像左上角為 0, 0)畫一條線段。 76 */ 77 for ($i=0; $i <5 ; $i++) 78 imageline($this->img,rand(0,$this->width),rand(0,$this->height),rand(0,$this->width),rand(0,$this->height),$this->randomColor()); 79 } 80 ////////////////////繪制驗證碼//////////////////// 81 private function createFont() 82 { 83 /*array imagettftext(resource $image,float $size,float $angle,int $x,int $y,int $color,string $fontfile,string $text) 84 *使用 TrueType 字體將 指定的text寫入圖像image。 85 *image,由圖象創建函數(例如 imagecreatetruecolor() )返回的圖象資源。 86 *size,字體的尺寸。 87 *angle,角度制表示的角度,0 度為從左向右讀的文本。更高數值表示逆時針旋轉。例如 90 度表示從下向上讀的文本。 88 *x和y,表示的坐標定義了第一個字符的基本點(大概是字符的左下角)。 89 *color,顏色索引。 90 *fontfile,想要使用的 TrueType 字體的路徑。 91 *text,UTF-8 編碼的文本字符串。 92 */ 93 for ($i=0;$i<$this->codelen;$i++) 94 { 95 $randomFZ=rand($this->fontsize,$this->fontsize+10); 96 $randomAngle=rand(-40,40); 97 $x=20+$this->fontsize*$i; 98 $y=40; 99 $fontcolor=imagecolorallocate($this->img,rand(0,156),rand(0,156),rand(0,156)); 100 imagettftext($this->img,$randomFZ,$randomAngle,$x,$y,$fontcolor,$this->font,$this->code[$i]); 101 } 102 } 103 ////////////////////輸出圖像並釋放內存//////////////////// 104 private function outPut() 105 { 106 /*輸出png格式圖像*/ 107 header("Content-Type:image/png"); 108 /*bool imagepng(resource $image[,string $filename]) 109 *將 GD 圖像流(image)以 PNG 格式輸出到標準輸出(通常為瀏覽器),或者如果用 filename 給出了文件名則將其輸出到該文件。 110 */ 111 imagepng($this->img); 112 /*bool imagedestroy(resource $image) 113 *釋放與 image 關聯的內存。 114 */ 115 imagedestroy($this->img); 116 } 117 ////////////////////生成完整二維碼//////////////////// 118 public function getvc() 119 { 120 $this->createBg();//生成畫布 121 $this->randomCode();//生成驗證碼 122 $this->createFont();//繪制驗證碼 123 $this->createNoise();//繪制噪點和幹擾線 124 $this->outPut();//輸出圖像並釋放內存 125 } 126 ////////////////////獲取驗證碼答案//////////////////// 127 public function getCode() 128 { 129 return $this->code; 130 } 131 } 132 ?>
驗證碼封裝類VerificationCode.class.php 技術分享
 1 <?PHP 
 2     /*
 3     *創建VerificationCode的實例對象,生成驗證碼圖片並將正確答案存入session
 4     */
 5     session_start();
 6     require_once "VerificationCode.class.php";
 7     $verificationCode=new VerificationCode();
 8     $verificationCode->getvc();
 9     $_SESSION["VerificationCode"] = $verificationCode->getCode();
10 ?>
captcha.php 技術分享
 1 <?PHP 
 2     /*獲取驗證碼答案*/
 3     session_start();
 4     if (isset($_SESSION[‘VerificationCode‘])) {
 5         $VerificationCode=$_SESSION[‘VerificationCode‘];
 6     } else {
 7         die("獲取驗證碼答案失敗!!");
 8     }
 9 ?>
10 <html>
11     <head>
12         <title>圖片驗證實例</title>
13         <meta http-equiv = "content-type" content="text/html; charset = utf-8 " >
14     </head> 
15     <body> 
16         <form action="" method="post">
17             <p>
18                 <!--驗證碼-->
19                 <img  title="點擊更換" src="captcha.php" align="absbottom" onclick="this.src=‘captcha.php?‘+Math.random();"></img>
20                 <input type="text" name="validate" value="" size=10> 
21                 <DIV class="error"><?PHP 
22                     $validate="";
23                     if(isset($_POST["validate"]))
24                     {
25                         $validate=$_POST["validate"];
26                         echo "您剛才輸入的是:".$_POST["validate"]."<br>";
27                         echo "正&nbsp確&nbsp答&nbsp案&nbsp是:".$VerificationCode."<br>";
28                         if($validate!=$VerificationCode)
29                             echo "<font color=red>輸入有誤</font>"; 
30                         else
31                             echo "<font color=green>通過驗證</font>"; 
32                     } 
33                 ?></DIV>
34             </p>
35             <p><input type="submit" value="確定"></p>
36         </form>
37     </body>
38 </html>
mainview.php

技術分享

技術分享

技術分享

驗證碼驗證實例