实现验证码类 vcode.class.php

<?php 
class Vcode
{
	private $width; // 宽
	private $height; // 高
	private $num; // 数量
	private $code; // 验证码
	private $im; // gd资源

	// 构造方法三个参数
	function __construct($width=80,$height=25,$num=4)
	{
		$this->width = $width;
		$this->height = $height;
		$this->num = $num;
		$this->code = $this->createCode();
	}

	// 获取字符串的验证码,用于存储在服务中
	function getCode(){
		return $this->code;
	}

	// 输出验证码
	function outimg(){
		// 1. 创建背景 (大小、颜色、边框)
		$this->createBg();
		// 2. 画字 (大小、字体颜色)
		$this->outString();
		// 3. 干扰元素(点、线)
		$this->setDisturbElement();
		// 4. 输出图像
		$this->printImg();
	}

	// 创建背景
	private function createBg(){
		// 创建资源
		$this->im = imagecreatetruecolor($this->width,$this->height);
		// 设置随机的背景颜色
		$bgColor  =  imagecolorallocate ( $this->im, rand(225,255), rand(225,255), rand(225,255) );
		// 填充背景颜色
		imagefill ( $this->im ,  0 ,  0 ,  $bgColor );

		$borderColor = imagecolorallocate($this->im,0,0,0);
		imagerectangle ( $this->im , 0 , 0 , $this->width-1 , $this->height-1 , $borderColor );
	}

	// 画字
	private function outString(){
		for ($i=0; $i < $this->num; $i++) { 
			$textColor =  imagecolorallocate ( $this->im , rand(0,128) , rand(0,128) , rand(0,128) );
			$fontsize = rand(10,16);

			$x = 3+($this->width/$this->num)*$i;
			$y = rand(imagefontheight($fontsize),$this->height-2)-3;
			imagechar( $this->im , $fontsize ,  $x , 3 ,  $this->code{$i}, $textColor );
		}
		
	}

	// 干扰元素
	private function setDisturbElement(){
		for ($i=0; $i < 100; $i++) {
			$dotColor = imagecolorallocate ( $this->im , rand(0,255) , rand(0,255) , rand(0,255) );
			imagesetpixel($this->im, rand(1,$this->width-2), rand(1,$this->height-2), $dotColor);
		}
		for ($i=0; $i < 10; $i++) {
			$lineColor = imagecolorallocate ( $this->im , rand(0,255) , rand(0,255) , rand(0,255) );
			imagearc($this->im, rand(-10,$this->width), rand(-10,$this->height+10), rand(30,300), rand(30,300), 55, 44, $lineColor);
		}
	}

	// 输出图像
	private function printImg(){
		header("Content-type:text/png");
		imagepng($this->im);
	}

	// 生成验证码字符串
	private function createCode(){
		$code = '';
		$codes = '3456789abcdefjhijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXY';
		for ($i=0; $i < $this->num; $i++) {
			$code .= $codes{rand(0,strlen($codes)-1)};
		}
		return $code;
	}

	function __destruct(){
		imagedestroy($this->im);
	}
}


 ?>

05-code.php

<?php
session_start();
include 'vcode.class.php';

// 构造方法
$vcode = new Vcode(80,25,4);

// 将验证码放到服务器自己的空间保存一份
$_SESSION['code'] = $vcode->getCode();

// 将验证码图片输出
$vcode->outimg();

 ?>

05-reg.php

<?php
session_start();

if(isset($_POST['dosubmit'])){
	if(strtolower($_POST['code']) === strtolower($_SESSION['code'])){
		echo "验证码输入正确<br/>";
	}else{
		echo "验证码输入错误<br/>";
	}
}	

?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>注册</title>
</head>
<body>
	<form action="05-reg.php" method="post">
		username: <input type="text" name="username"> <br/>
		password: <input type="password" name="password"><br/>
		code: <input type="text" name="code"><img onclick="this.src='05-code.php?'+Math.random()" src="05-code.php"><br/>
		<input type="submit" name="dosubmit" value="登 录">
	</form>
</body>
</html>