細說php中的驗證碼類創建
我這裡自己寫了一個驗證碼類,我來演示一下怎麼使用,我是菜鳥一枚,大神請略過。我來講解一下它的使用方法,總共需要兩步即可。
第一步:
下載我制作好的驗證碼類。下載地址:http://files.cnblogs.com/files/xfjpeter/Verify.zip
第二步:
1.創建一個字的驗證碼文件
1 <?php
2
3 #引入驗證碼類文件
4 require_once('Verify.class.php');
5
6 #實例化驗證碼類
7 #初始化的使用可以傳四個參數,分別是:驗證碼圖片的長、高,驗證碼的長度,驗證碼的類型(驗證碼的類型需要將bgRand屬性設置為false)
8 $code = new Verify(140, 40, 6, 6);
9
10 #設置驗證碼圖片的長度
11 $code -> width = 200;
12
13 #設置驗證碼圖片的高度
14 $code -> height = 60;
15
16 #是否隨機背景,默認true(隨機)
17 $code -> bgRand = false;
18
19 #顯示驗證碼
20 $code -> verify();
生成的圖片樣式為如圖
2.驗證碼類文件為
1 <?php
2
3 /**
4 * 驗證碼類
5 * @author John <fsyzxz@163.com>
6 */
7 class Verify
8 {
9 private $width = 160; //驗證碼的寬度
10 private $height = 60; //驗證碼的高度
11 private $type = 1; //驗證碼的類型
12 private $length = 4; //驗證碼的長度
13 private $code; //驗證碼
14 private $img; //圖像的資源
15 private $seKey = 'John'; //密鑰
16 private $bgRand = true; //隨機背景圖片
17
18 /**
19 * 構造函數
20 * @param type $width 驗證碼的寬度
21 * @param type $height 驗證碼的高度
22 * @param type $length 驗證碼的長度
23 * @param type $type 驗證碼的類型
24 */
25 public function __construct($width = 160, $height = 40, $length = 4, $type = 1)
26 {
27 $this->width = !empty($width) ? $width : $this->width;
28 $this->height = !empty($height) ? $height : $this->height;
29 $this->length = !empty($length) ? $length : $this->length;
30 $this->type = !empty($type) ? $type : $this->type;
31 }
32
33 /**
34 * 設置屬性值
35 * @param type $name 屬性名
36 * @param type $value 屬性值
37 */
38 public function __set($name, $value)
39 {
40 if (isset($name)) {
41 $this->$name = $value;
42 }
43 }
44
45 /**
46 * 獲取屬性值
47 * @param type $name 屬性名
48 * @return type 返回屬性值
49 */
50 public function __get($name) {
51 return $this->$name;
52 }
53
54 /**
55 * 校驗驗證碼
56 * @param type $code 表單提供的驗證碼
57 * @return boolean
58 */
59 public function check($code){
60 if (!isset($_SESSION)) {session_start();}
61 if ($this->encodeVerify(strtolower($code)) === $_SESSION['code']){
62 return true;
63 }else{
64 return false;
65 }
66 }
67
68 //輸出驗證碼
69 public function verify()
70 {
71 $this->code = $this->createVerify();
72 //創建背景
73 $this->createBackground();
74 //文字顯示
75 $this->writeString();
76 //畫干擾線
77 $this->paitLine();
78 //輸入圖像
79 $this->printImg();
80 }
81
82 /**
83 * 創建背景圖片
84 */
85 private function createBackground()
86 {
87 //從圖片庫創建一個圖像, 判斷是否隨機
88 if ($this->bgRand){
89 $img = imagecreatefromjpeg('./verify/bgs/'.mt_rand(1,8).'.jpg');
90 }else{
91 $img = imagecreatefromjpeg('./verify/bgs/'.$this->type.'.jpg');
92 }
93 //創建一個圖片
94 $this->img = imagecreatetruecolor($this->width, $this->height);
95 //把圖片復制到創建的圖像上
96 imagecopyresampled($this->img, $img, 0, 0, 0, 0, $this->width, $this->height, imagesx($img), imagesy($img));
97 }
98
99 /**
100 * 在圖片上寫字
101 */
102 private function writeString()
103 {
104 $color = imagecolorallocatealpha($this->img, mt_rand(0,128), mt_rand(0,128), mt_rand(0,128), 0);
105 $fontType = './verify/ttfs/'.mt_rand(1,6).'.ttf';
106 $fontSize = mt_rand(15, 20);
107 for ($i = 0; $i < $this->length; $i++) {
108 $x = 3+($this->width/$this->length)*$i;
109 $y = mt_rand(($this->height/3)*2, ($this->height/3)*2);
110 //把驗證碼寫在圖片上
111 imagettftext($this->img, $fontSize, 0, $x, $y, $color, $fontType, $this->code[$i]);
112 }
113 }
114
115 /**
116 * 畫干擾線和字母
117 */
118 private function paitLine()
119 {
120 $px = $py = 0;
121 $codes = '2345678abcdefhijkmnpqrstuvwxyz';
122 for ($i = 0; $i < $this->width/4; $i++){
123 $num = mt_rand(0, strlen($codes)-1);
124 $color = imagecolorallocatealpha($this->img, 255, 255, 255, 80);
125 //畫字母
126 imagechar($this->img, 8, mt_rand(3, $this->width), mt_rand(3, $this->height), $codes{$num}, $color);
127 }
128 }
129
130 /**
131 * 輸入圖像
132 */
133 private function printImg()
134 {
135 if(function_exists('imagegif')){
136 // 針對 GIF
137 header('Content-Type: image/gif');
138 imagegif($this->img);
139 }elseif(function_exists('imagejpeg')){
140 // 針對 JPEG
141 header('Content-Type: image/jpeg');
142 imagejpeg($this->img, NULL, 100);
143 }elseif(function_exists('imagepng')){
144 // 針對 PNG
145 header('Content-Type: image/png');
146 imagepng($this->img);
147 }elseif(function_exists('imagewbmp')){
148 // 針對 WBMP
149 header('Content-Type: image/vnd.wap.wbmp');
150 imagewbmp($this->img);
151 }
152 }
153
154 /**
155 * 生成驗證碼
156 * @return string 返回生成的驗證碼
157 */
158 private function createVerify()
159 {
160 $codeSet = '2345678abcdefhijkmnpqrstuvwxyz';
161 $codes = '';
162 for ($i = 0; $i < $this->length; $i++) {
163 $codes .= $codeSet[mt_rand(0, strlen($codeSet)-1)];
164 }
165 //把驗證碼保存到session中
166 if (!isset($_SESSION)) {session_start();}
167 $_SESSION['code'] = $this->encodeVerify(strtolower($codes));
168 // $_SESSION['code'] = $codes;
169 return $codes;
170 }
171
172 /**
173 * 加密驗證碼
174 * @param type $string
175 * @return type
176 */
177 private function encodeVerify($string)
178 {
179 $key = substr(md5($this->seKey), 5, 8);
180 $str = substr(md5($string), 8, 10);
181 return md5($key . $str);
182 }
183
184 /**
185 * 銷毀圖像
186 */
187 function __destruct()
188 {
189 if (isset($this->img)){
190 imagedestroy($this->img);
191 }
192 }
193 }
以上兩步即可生生你想要的驗證。
另外說明,Verify.class.php中有一個驗證驗證碼是否正確的方法,使用如下
將你從界面中獲得的驗證碼傳入code方法中即可
if ($code -> code(這是傳入你頁面中獲取的驗證碼值)){
#這是驗證正確的操作
}else{
#驗證失敗的操作
}
以上就是我創建整個驗證碼的心得,希望對點擊進來看的人有幫助。