PHP制作验证码详细教程
效果:
myvcode.class.php:封装创建验证码的类
<span><span id="lnum1"> 1:</span> <?php</span>
<span><span id="lnum2"> 2:</span> <span>/*</span></span>
<span><span id="lnum3"> 3:</span> <span>* file:myvcode.class.php</span></span>
<span><span id="lnum4"> 4:</span> <span>* 验证码类,类名Vcode</span></span>
<span><span id="lnum5"> 5:</span> <span>*/</span></span>
<span><span id="lnum6"> 6:</span> <span>class</span> Vcode</span>
<span><span id="lnum7"> 7:</span> {</span>
<span><span id="lnum8"> 8:</span> <span>private</span> $width; <span>/*验证码宽度*/</span></span>
<span><span id="lnum9"> 9:</span> <span>private</span> $height; <span>/*验证码高度*/</span></span>
<span><span id="lnum10"> 10:</span> <span>private</span> $codeNum; <span>/*验证码字符个数*/</span></span>
<span><span id="lnum11"> 11:</span> <span>private</span> $checkCode; <span>/*验证码字符*/</span></span>
<span><span id="lnum12"> 12:</span> <span>private</span> $image; <span>/*验证码资源*/</span></span>
<span><span id="lnum13"> 13:</span> <span>private</span> $pixNum; <span>/*绘制干扰点的个数*/</span></span>
<span><span id="lnum14"> 14:</span> <span>private</span> $lineNum; <span>/*绘制干扰线的条数*/</span></span>
<span><span id="lnum15"> 15:</span></span>
<span><span id="lnum16"> 16:</span> <span>/*</span></span>
<span><span id="lnum17"> 17:</span> <span> *构造方法实例化验证码对象,并初始化数据</span></span>
<span><span id="lnum18"> 18:</span> <span> *@param int $width 设置默认宽度</span></span>
<span><span id="lnum19"> 19:</span> <span> *@param int $height 设置默认高度</span></span>
<span><span id="lnum20"> 20:</span> <span> *@param int $codeNum 设置验证码中的字符个数</span></span>
<span><span id="lnum21"> 21:</span> <span> *@param int $pixNum 设置干扰点的个数</span></span>
<span><span id="lnum22"> 22:</span> <span> *@param int $lineNum 设置干扰线的数量</span></span>
<span><span id="lnum23"> 23:</span> <span> */</span></span>
<span><span id="lnum24"> 24:</span> <span>function</span> __construct($width=80,$height=40,$codeNum=4,$pixNum=40,$lineNum=5)</span>
<span><span id="lnum25"> 25:</span> {</span>
<span><span id="lnum26"> 26:</span> $this->width = $width;</span>
<span><span id="lnum27"> 27:</span> $this->height = $height;</span>
<span><span id="lnum28"> 28:</span> $this->codeNum = $codeNum;</span>
<span><span id="lnum29"> 29:</span> $this->pixNum = $pixNum;</span>
<span><span id="lnum30"> 30:</span> $this->lineNum = $lineNum;</span>
<span><span id="lnum31"> 31:</span> }</span>
<span><span id="lnum32"> 32:</span> <span>/*内部私有方法,创建图像资源*/</span></span>
<span><span id="lnum33"> 33:</span> <span>private</span> <span>function</span> getCreateImage()</span>
<span><span id="lnum34"> 34:</span> {</span>
<span><span id="lnum35"> 35:</span> $this->image = imagecreatetruecolor($this->width, $this->height);</span>
<span><span id="lnum36"> 36:</span> $white = imagecolorallocate($this->image,0xff,0xff,0xff);</span>
<span><span id="lnum37"> 37:</span> imagefill($this->image, 0, 0, $white);</span>
<span><span id="lnum38"> 38:</span> $black = imagecolorallocate($this->image,0,0,0);</span>
<span><span id="lnum39"> 39:</span> imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $black);</span>
<span><span id="lnum40"> 40:</span> }</span>
<span><span id="lnum41"> 41:</span> <span>/*内部私有方法,绘制字符,去掉o0Llz和012*/</span></span>
<span><span id="lnum42"> 42:</span> <span>private</span> <span>function</span> createCheckCode()</span>
<span><span id="lnum43"> 43:</span> {</span>
<span><span id="lnum44"> 44:</span> $code = <span>'3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY'</span>;</span>
<span><span id="lnum45"> 45:</span> $this->checkCode = <span>""</span>;</span>
<span><span id="lnum46"> 46:</span> <span>for</span>($i=0; $i<$this->codeNum;$i++)</span>
<span><span id="lnum47"> 47:</span> {</span>
<span><span id="lnum48"> 48:</span> $char = $code{rand(0,strlen($code) - 1)};</span>
<span><span id="lnum49"> 49:</span> $this->checkCode .= $char;</span>
<span><span id="lnum50"> 50:</span> $fontColor = imagecolorallocate($this->image, rand(0,128), rand(0,128),rand(0,128));</span>
<span><span id="lnum51"> 51:</span> $fontSize = rand(3,5);</span>
<span><span id="lnum52"> 52:</span> $x = rand(0,$this->width-imagefontwidth($fontSize));</span>
<span><span id="lnum53"> 53:</span> $y = rand(0,$this->height-imagefontheight($fontSize));</span>
<span><span id="lnum54"> 54:</span> imagechar($this->image, $fontSize, $x, $y, $char, $fontColor);</span>
<span><span id="lnum55"> 55:</span> }</span>
<span><span id="lnum56"> 56:</span> }</span>
<span><span id="lnum57"> 57:</span> <span>/*内部私有方法设置干扰元素*/</span></span>
<span><span id="lnum58"> 58:</span> <span>private</span> <span>function</span> setDisturbColor()</span>
<span><span id="lnum59"> 59:</span> {</span>
<span><span id="lnum60"> 60:</span> <span>/*绘制干扰点*/</span></span>
<span><span id="lnum61"> 61:</span> <span>for</span>($i=0; $i<$this->pixNum; $i++)</span>
<span><span id="lnum62"> 62:</span> {</span>
<span><span id="lnum63"> 63:</span> $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));</span>
<span><span id="lnum64"> 64:</span> imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);</span>
<span><span id="lnum65"> 65:</span> }</span>
<span><span id="lnum66"> 66:</span> <span>/*绘制干扰线*/</span></span>
<span><span id="lnum67"> 67:</span> <span>for</span>($i=0; $i<$this->lineNum; $i++)</span>
<span><span id="lnum68"> 68:</span> {</span>
<span><span id="lnum69"> 69:</span> $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));</span>
70: imageline($this->image, rand(1,$this->width / 2), rand(1,$this->height / 2),
<p><span>rand($this->width / 2,$this->width – 2), rand($this->height / 2,$this->height – 2), $color);</span></p>
<span><span id="lnum71"> 71:</span> }</span>
<span><span id="lnum72"> 72:</span> }</span>
<span><span id="lnum73"> 73:</span> <span>/*开启session保存 利用echo 输出图像*/</span></span>
<span><span id="lnum74"> 74:</span> <span>function</span> __toString()</span>
<span><span id="lnum75"> 75:</span> {</span>
<span><span id="lnum76"> 76:</span> $_SESSION[<span>'code'</span>] = strtoupper($this->checkCode);</span>
<span><span id="lnum77"> 77:</span> $this->getCreateImage();</span>
<span><span id="lnum78"> 78:</span> $this->createCheckCode();</span>
<span><span id="lnum79"> 79:</span> $this->setDisturbColor();</span>
<span><span id="lnum80"> 80:</span> $this->outputImg();</span>
<span><span id="lnum81"> 81:</span> }</span>
<span><span id="lnum82"> 82:</span> <span>/*内部私有方法输出图像*/</span></span>
<span><span id="lnum83"> 83:</span> <span>private</span> <span>function</span> outputImg()</span>
<span><span id="lnum84"> 84:</span> {</span>
<span><span id="lnum85"> 85:</span> header(<span>"content-type:image/png"</span>);</span>
<span><span id="lnum86"> 86:</span> imagepng($this->image);</span>
<span><span id="lnum87"> 87:</span> }</span>
<span><span id="lnum88"> 88:</span> <span>/*析构方法,释放对象*/</span></span>
<span><span id="lnum89"> 89:</span> <span>function</span> __destruct()</span>
<span><span id="lnum90"> 90:</span> {</span>
<span><span id="lnum91"> 91:</span> imagedestroy($this->image);</span>
<span><span id="lnum92"> 92:</span> }</span>
<span><span id="lnum93"> 93:</span> }</span>
<span><span id="lnum94"> 94:</span> ?></span>
imgcode.php输出图像
<span><span id="lnum1"> 1:</span> <?php</span>
<span><span id="lnum2"> 2:</span> session_start();</span>
<span><span id="lnum3"> 3:</span> <span>require_once</span>(<span>'myvcode.class.php'</span>);</span>
<span><span id="lnum4"> 4:</span> <span>echo</span> <span>new</span> Vcode();</span>
<span><span id="lnum5"> 5:</span> ?></span>
test.html:同过img标签引用
<span><span id="lnum1"> 1:</span> <span><</span><span>img</span> <span>src</span><span>="imgcode.php"</span><span>></span></span>
可以加一个a标签,用js实现换一张效果:
<span>/*局部刷新换验证码*/ function changeCode() { var imgcode = document.getElementById('code'); var change = document.getElementById('change'); change.onclick = function() { /*必须加后面的参数才能刷新*/ imgcode.src='code.php?tm'+Math.random(); } }</span>
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);...余下全文>>