PHP制作驗證碼詳細教程
效果:
myvcode.class.php:封裝創建驗證碼的類
1: <?php
2: /*
3: * file:myvcode.class.php
4: * 驗證碼類,類名Vcode
5: */
6: class Vcode
7: {
8: private $width; /*驗證碼寬度*/
9: private $height; /*驗證碼高度*/
10: private $codeNum; /*驗證碼字符個數*/
11: private $checkCode; /*驗證碼字符*/
12: private $image; /*驗證碼資源*/
13: private $pixNum; /*繪制干擾點的個數*/
14: private $lineNum; /*繪制干擾線的條數*/
15:
16: /*
17: *構造方法實例化驗證碼對象,並初始化數據
18: *@param int $width 設置默認寬度
19: *@param int $height 設置默認高度
20: *@param int $codeNum 設置驗證碼中的字符個數
21: *@param int $pixNum 設置干擾點的個數
22: *@param int $lineNum 設置干擾線的數量
23: */
24: function __construct($width=80,$height=40,$codeNum=4,$pixNum=40,$lineNum=5)
25: {
26: $this->width = $width;
27: $this->height = $height;
28: $this->codeNum = $codeNum;
29: $this->pixNum = $pixNum;
30: $this->lineNum = $lineNum;
31: }
32: /*內部私有方法,創建圖像資源*/
33: private function getCreateImage()
34: {
35: $this->image = imagecreatetruecolor($this->width, $this->height);
36: $white = imagecolorallocate($this->image,0xff,0xff,0xff);
37: imagefill($this->image, 0, 0, $white);
38: $black = imagecolorallocate($this->image,0,0,0);
39: imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $black);
40: }
41: /*內部私有方法,繪制字符,去掉o0Llz和012*/
42: private function createCheckCode()
43: {
44: $code = '3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY';
45: $this->checkCode = "";
46: for($i=0; $i<$this->codeNum;$i++)
47: {
48: $char = $code{rand(0,strlen($code) - 1)};
49: $this->checkCode .= $char;
50: $fontColor = imagecolorallocate($this->image, rand(0,128), rand(0,128),rand(0,128));
51: $fontSize = rand(3,5);
52: $x = rand(0,$this->width-imagefontwidth($fontSize));
53: $y = rand(0,$this->height-imagefontheight($fontSize));
54: imagechar($this->image, $fontSize, $x, $y, $char, $fontColor);
55: }
56: }
57: /*內部私有方法設置干擾元素*/
58: private function setDisturbColor()
59: {
60: /*繪制干擾點*/
61: for($i=0; $i<$this->pixNum; $i++)
62: {
63: $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
64: imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);
65: }
66: /*繪制干擾線*/
67: for($i=0; $i<$this->lineNum; $i++)
68: {
69: $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
70: imageline($this->image, rand(1,$this->width / 2), rand(1,$this->height / 2),
rand($this->width / 2,$this->width – 2), rand($this->height / 2,$this->height – 2), $color);
71: }
72: }
73: /*開啟session保存 利用echo 輸出圖像*/
74: function __toString()
75: {
76: $_SESSION['code'] = strtoupper($this->checkCode);
77: $this->getCreateImage();
78: $this->createCheckCode();
79: $this->setDisturbColor();
80: $this->outputImg();
81: }
82: /*內部私有方法輸出圖像*/
83: private function outputImg()
84: {
85: header("content-type:image/png");
86: imagepng($this->image);
87: }
88: /*析構方法,釋放對象*/
89: function __destruct()
90: {
91: imagedestroy($this->image);
92: }
93: }
94: ?>
imgcode.php輸出圖像
1: <?php
2: session_start();
3: require_once('myvcode.class.php');
4: echo new Vcode();
5: ?>
test.html:同過img標簽引用
1: <img src="imgcode.php">
可以加一個a標簽,用js實現換一張效果:
/*局部刷新換驗證碼*/ function changeCode() { var imgcode = document.getElementById('code'); var change = document.getElementById('change'); change.onclick = function() { /*必須加後面的參數才能刷新*/ imgcode.src='code.php?tm'+Math.random(); } }
code和change分別是img和a的id
很簡單,你用[pic]時PHP會檢查是否有一個名為pic的常量。這肯定找不到,於是它就在文件開頭輸出一個警告信息。此信息在圖片數據前輸出,導致圖片無法被浏覽器識別。解決方法是給pic加引號,$_SESSION['pic']
可以用php的GD庫做
//隨機生成驗證碼
class randomString
{
function createRandomStr($strLen)
{
list($usec, $sec) = explode(' ', microtime());
(float) $sec + ((float) $usec * 100000);
$number = '';
$number_len = $strLen;
$stuff = '1234567890abcdefghijklmnopqrstuvwxyz';//附加碼顯示范圍ABCDEFGHIJKLMNOPQRSTUVWXYZ
$stuff_len = strlen($stuff) - 1;
for ($i = 0; $i < $number_len; $i++) {
$number .= substr($stuff, mt_rand(0, $stuff_len), 1);
}
return $number;
}
}
通過ZD庫將驗證碼變成圖片
$number = $createStr->createRandomStr('4');//驗證碼的位數
$number_len = strlen($number);
$_SESSION["VERIFY_CODE"] = $number;
// 生成驗證碼圖片
$img_width = 60;
$img_height = 20;
$img = imageCreate($img_width, $img_height);
ImageColorAllocate($img, 0x6C, 0x74, 0x70);
$white = ImageColorAllocate($img, 0xff, 0xff, 0xff);
$ix = 6;
$iy = 2;
for ($i = 0; $i < $number_len; $i++) {
imageString($img, 5, $ix, $iy, $number[$i], $white);
$ix += 14;
}
for($i=0;$i<200;$i++) //加入干擾象素
{
$randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($img, rand()%100 , rand()%50 , $randcolor);
}
// 輸出圖片
header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));
imagepng($img);
imagedestroy($img);...余下全文>>