验证码是为了防止机器灌水给网站带来污染以及增加服务器负担而出现的。目前大大小小的网站都有验证码。今天自己实现了一个简单的验证码类。说简单是因为没有加一些干扰的弧线等等,只是将文字旋转了一下。当然,因为字体的原因,要想一眼看出来并不容易。同时,为了避免字母的大小写与数字混淆,又去掉了那些看起来很像的字母数字。
类:
<?php
/**
*简单生成验证码类
*/
class Captcha {
private $width;//验证码宽度
private $height;//验证码高度
private $countOfChars;//字符数
//private $distrubLines;//干扰线条数
private $chars;//随机生成的字符串
public function __construct($width=100,$height=30,$countOfChars=4,$distrubLines=2) {
//初始化参数
$this->width=$width;
$this->height=$height;
$this->countOfChars=$countOfChars;
session_start();
}
/**
* 执行全部动作,生成验证码并直接输出
*/
public function execute(){
$imageHandle=$this->createImage();
$this->createChars();
$this->drawChars($imageHandle);
$this->outImage($imageHandle);
}
/**
* 创建画布,并随机填充颜色
* @return 返回画布句柄
*/
public function createImage(){
$imageHandle= imagecreate($this->width, $this->height);
//随机背景颜色
$randColor=imagecolorallocate($imageHandle, 50, mt_rand(0, 50), mt_rand(0, 50));
imagefill($imageHandle, 0, 0, $randColor);
return $imageHandle;
}
/**
* 生成随机字符
*/
private function createChars(){
//候选字符
$str='ABCDEFGHJKLMNPQRSTUVWXZabcdefghijkmnpqtuvwx2346789';
$chars='';
for($i=0;$i<$this->countOfChars;$i++){
$chars.=$str[mt_rand(0,strlen($str)-1)];
}
$this->chars=$chars;
//保存在会话中
$_SESSION['captcha']=strtolower($chars);
}
/**
* 将字符写入图像
* @param type $imageHandle 图像句柄
*/
private function drawChars($imageHandle){
if($this->chars!=null){
$font='/home/WWW/YuWeiLiShuFT.ttf';
for($i=0;$i<strlen($this->chars);$i++){
$color= imagecolorallocate($imageHandle,mt_rand(50, 200),mt_rand(100, 255),255);
imagefttext($imageHandle,30, 30,$i*20+10,25,$color,$font,$this->chars[$i]);
}
}
}
/**
* 输出图像
* @param type $imageHandle 图像句柄
*/
private function outImage($imageHandle){
imagepng($imageHandle);
imagedestroy($imageHandle);
}
/**
* 判断用户输入的验证码是否正确
* @param type $usrInput 用户的输入
* @return boolean 验证码是否匹配
*/
public static function isRight($usrInput){
if(isset($_SESSION['captcha'])){
if(strtolower($usrInput)==$_SESSION['captcha']){
$_SESSION['captcha']=null;
return true;
}else{
$_SESSION['captcha']=null;
return false;
}
}
}
}
把验证设置成了静态方法,因为生成验证码后已经把验证码存到了session中,验证时直接调用静态方法,而不需要实例化这个类了。







