PHP制作验证码,php验证码
PHP制作验证码,php验证码
PHP制作验证码详细教程
效果:
myvcode.class.php:封装创建验证码的类
1:
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; $icodeNum;$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; $ipixNum; $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; $ilineNum; $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:
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 .= 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 imageString($img, 5, $ix, $iy, $number[$i], $white);
$ix += 14;
}
for($i=0;$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);...余下全文>>
上一篇: MySQL如何从表中取出随机数据