程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 登錄頁面動態驗證碼的設置,頁面動態驗證碼

登錄頁面動態驗證碼的設置,頁面動態驗證碼

編輯:關於PHP編程

登錄頁面動態驗證碼的設置,頁面動態驗證碼


登錄頁面動態驗證碼的設置

采用php中創建對象的思想進行動態驗證碼的設置

1、創建出一個背景圖片,用來存放動態碼輸出位置

 1       function createImage(){
 2         // 創建圖片對象,並設置圖片的寬高 imagecreatetruecolor
 3         $this->image = imagecreatetruecolor($this->width, $this->height);
 4         // 圖片創建背景顏色
 5         // rand(下界,上界), php中的隨機數
 6         $backgroundColor = imagecolorallocate($this->image, rand(225,255), rand(225, 255), rand(225,255));
 7         // 填充背景顏色
 8         imagefill($this->image, 0, 0, $backgroundColor);
 9         // 給驗證碼設置邊框
10         $borderColor = imagecolorallocate($this->image, 0, 0 , 0); // 黑色邊框
11         // 繪制邊框
12         imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $borderColor);
13       }

2、設置動態碼中的干擾信息,可以為(點, 線·······)

 

function setDisturbElement(){
        // 隨機生成干擾項的數量
        $lineNumbers = rand(2, 4);
        // 通過for循環循環生成每個干擾線
        for ($i=0; $i < $lineNumbers; $i++) {
          // 左上角到右下角
          $x1 = rand(2, $this->width / 2);
          $y1 = rand(2, $this->height / 2);
          $x2 = rand($this->width/2, $this->width-2);
          $y2 = rand($this->height/2, $this->height-2);
          $color = imagecolorallocate($this->image, rand(100, 200), rand(100, 200), rand(100, 200));
          // 繪制線條
          imageline($this->image, $x1, $y1, $x2, $y2, $color);
        }
      }

 

 

 

 

3、生成驗證碼字符串中的字符串信息,並將其存儲到本地的session中以便於信息的驗證

 

 1 private function createCheckingCode(){
 2         $code = "0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDASZXCVBNM";
 3         // echo $code;
 4         // 接受驗證碼的字符串
 5         $string = '';
 6         for ($i=0; $i < $this->codeNumbers; $i++) {
 7             $index = rand(0, (strlen($code) - 1));
 8             // echo $index;
 9             $string .= substr($code, $index, 1); // 獲取code字符串的下標為$index的字符,添加到String字符串中
10         }
11         return $string;
12       }
13       // 為圖像設置驗證碼文本
14       function setValidationCode(){
15         for ($i=0; $i < $this->codeNumbers; $i++) {
16             $x = rand(1, 4) + $this->width * $i / $this->codeNumbers;
17             $y = rand(1, $this->height/4);
18             $color= imagecolorallocate($this->image, rand(0,128), rand(0, 128), rand(0, 128));
19             $fontSize = rand(6, 8);
20             // 繪制文本
21             imagestring($this->image,$fontSize, $x, $y, $this->checkingCode[$i], $color);
22         }
23       }
24       // 存儲驗證碼到session中
25       function saveVcodeToSession(){
26           include_once('session.php');
27           $session  = new Session();
28           $session->set('checkingCode', $this->checkingCode, 10);
29       }

 

 

 

 

 

4、對生成的驗證碼進行前端輸出

 

1 function outputImage(){
2         $this->createImage(); // 繪制圖片
3         $this->setDisturbElement(); // 繪制線條
4         $this->setValidationCode();  // 生成文本
5         header("Content-Type:image/png"); // 設置文檔輸出類型, png類型 | jpg
6         imagepng($this->image);
7       }

 

 

完整代碼

 

 1 <?php
 2     // 要將驗證碼的碼值保存在session,方便前端進行比對驗證
 3     session_start();
 4     /**
 5      * 驗證碼生成類
 6      */
 7     class Validationcode
 8     {
 9       // private ,私有成員,只限於內部使用
10       private $width;
11       private $height;
12       private $codeNumbers; // 驗證碼的個數
13       private $image; // 將來要輸出的圖像
14       private $checkingCode; // 驗證碼字符串,將被保存在session數組中
15       function __construct($width = 80, $height = 30, $codeNumbers = 4) // 如果不提供參數,則80, 30, 4 為默認值
16       {
17           $this->width        = $width;
18           $this->height       = $height;
19           $this->codeNumbers  = $codeNumbers;
20           $this->checkingCode = $this->createCheckingCode(); // 隨機生成驗證碼
21           // echo $this->checkingCode;
22       }
23       // 創建背景圖片函數
24       function createImage(){
25         // 創建圖片對象,並設置圖片的寬高 imagecreatetruecolor
26         $this->image = imagecreatetruecolor($this->width, $this->height);
27         // 圖片創建背景顏色
28         // rand(下界,上界), php中的隨機數
29         $backgroundColor = imagecolorallocate($this->image, rand(225,255), rand(225, 255), rand(225,255));
30         // 填充背景顏色
31         imagefill($this->image, 0, 0, $backgroundColor);
32         // 給驗證碼設置邊框
33         $borderColor = imagecolorallocate($this->image, 0, 0 , 0); // 黑色邊框
34         // 繪制邊框
35         imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $borderColor);
36       }
37       // 設置干擾項
38       function setDisturbElement(){
39         // 隨機生成干擾項的數量
40         $lineNumbers = rand(2, 4);
41         // 通過for循環循環生成每個干擾線
42         for ($i=0; $i < $lineNumbers; $i++) {
43           // 左上角到右下角
44           $x1 = rand(2, $this->width / 2);
45           $y1 = rand(2, $this->height / 2);
46           $x2 = rand($this->width/2, $this->width-2);
47           $y2 = rand($this->height/2, $this->height-2);
48           $color = imagecolorallocate($this->image, rand(100, 200), rand(100, 200), rand(100, 200));
49           // 繪制線條
50           imageline($this->image, $x1, $y1, $x2, $y2, $color);
51         }
52       }
53       // 對創建的驗證碼圖片進行輸出
54       function outputImage(){
55         $this->createImage(); // 繪制圖片
56         $this->setDisturbElement(); // 繪制線條
57         $this->setValidationCode();  // 生成文本
58         header("Content-Type:image/png"); // 設置文檔輸出類型, png類型 | jpg
59         imagepng($this->image);
60       }
61       // 生成驗證碼字符串
62       // 私有的函數
63       private function createCheckingCode(){
64         $code = "0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDASZXCVBNM";
65         // echo $code;
66         // 接受驗證碼的字符串
67         $string = '';
68         for ($i=0; $i < $this->codeNumbers; $i++) {
69             $index = rand(0, (strlen($code) - 1));
70             // echo $index;
71             $string .= substr($code, $index, 1); // 獲取code字符串的下標為$index的字符,添加到String字符串中
72         }
73         return $string;
74       }
75       // 為圖像設置驗證碼文本
76       function setValidationCode(){
77         for ($i=0; $i < $this->codeNumbers; $i++) {
78             $x = rand(1, 4) + $this->width * $i / $this->codeNumbers;
79             $y = rand(1, $this->height/4);
80             $color= imagecolorallocate($this->image, rand(0,128), rand(0, 128), rand(0, 128));
81             $fontSize = rand(6, 8);
82             // 繪制文本
83             imagestring($this->image,$fontSize, $x, $y, $this->checkingCode[$i], $color);
84         }
85       }
86       // 存儲驗證碼到session中
87       function saveVcodeToSession(){
88           include_once('session.php');
89           $session  = new Session();
90           $session->set('checkingCode', $this->checkingCode, 10);
91       }
92     }
93     $vcode = new Validationcode();
94     $vcode->outputImage();
95     $vcode->saveVcodeToSession();
96  ?>
php

 

 

 

 

 

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved