PHP code 验证码生成类定义和简单使用示例
本文实例讲述了PHPcode验证码生成类定义和简单使用。分享给大家供大家参考,具体如下:
code.php
number=$number;
$this->codeType=$codeType;
$this->width=$width;
$this->height=$height;
$this->code=$this->createCode();
}
/**
*销毁资源
*/
publicfunction__destruct()
{
imagedestroy($this->image);
}
/**
*外部调用code时触发
*@param$name
*@returnbool
*/
publicfunction__get($name)
{
if('code'==$name){
return$this->$name;
}else{
returnfalse;
}
}
/**
*生成code
*/
protectedfunctioncreateCode()
{
switch($this->codeType){
case0:
$code=$this->getNum();
break;
case1:
$code=$this->getChar();
break;
case2:
$code=$this->getNumChar();
break;
default:
die('样式不对');
}
return$code;
}
/**
*数字验证码
*@returnstring
*/
protectedfunctiongetNum()
{
$str=join('',range(0,9));
returnsubstr(str_shuffle($str),0,$this->number);
}
/**
*字符验证码
*@returnstring
*/
protectedfunctiongetChar()
{
$str=join('',range('a','z'));
$str=$str.strtoupper($str);
returnsubstr(str_shuffle($str),0,$this->number);
}
/**
*字符和数字混合验证码
*@returnstring
*/
protectedfunctiongetNumChar()
{
$num=join('',range(0,9));
$str=join('',range('a','z'));
$str_big=strtoupper($str);
$numChar=$num.$str.$str_big;
returnsubstr(str_shuffle($numChar),0,$this->number);
}
/**
*生成图像
*/
protectedfunctioncreateImage()
{
$this->image=imagecreatetruecolor($this->width,$this->height);
}
/**
*填充背景色
*/
protectedfunctionfillColor()
{
imagefill($this->image,0,0,$this->lightColor());
}
/**
*浅颜色
*@returnint
*/
protectedfunctionlightColor()
{
returnimagecolorallocate($this->image,mt_rand(170,255),mt_rand(170,255),mt_rand(170,255));
}
/**
*深颜色
*@returnint
*/
protectedfunctiondarkColor()
{
returnimagecolorallocate($this->image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
}
/**
*添加验证码字符
*/
protectedfunctiondrawChar()
{
$width=ceil($this->width/$this->number);
for($i=0;$i<$this->number;$i++){
$x=mt_rand($i*($width-5),($i+1)*($width-5));
$y=mt_rand(0,$this->height-15);
imagechar($this->image,5,$x,$y,$this->code[$i],$this->darkColor());
}
}
/**
*添加干扰点
*/
protectedfunctiondrawDisturb()
{
for($i=0;$i<100;$i++){
imagesetpixel($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->darkColor());
}
}
/**
*添加干扰线
*/
protectedfunctiondrawArc()
{
for($i=0;$i<$this->number-3;$i++){
imagearc($this->image,mt_rand(5,$this->width),mt_rand(5,$this->height),mt_rand(5,$this->width),mt_rand(5,$this->height),mt_rand(0,70),mt_rand(300,360),$this->darkColor());
}
}
/**
*输出显示
*/
protectedfunctionshow()
{
header('Content-Type:image/png');
imagepng($this->image);
}
/**
*外部image
*/
publicfunctionoutImage()
{
$this->createImage();//创建画布
$this->fillColor();//填充背景色
$this->drawChar();//添加验证字符
$this->drawDisturb();//添加干扰点
$this->drawArc();//添加干扰线
$this->show();//输出
}
}
展示验证码。。保存验证码和过期时间
outImage(); session_start(); $_SESSION['code']=[ 'code'=>$code->code, 'exp_time'=>time()+(60*60*10), ];
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。