一个好用的PHP验证码类实例分享


Posted in PHP onDecember 27, 2013

分享一个好用的php验证码类,包括调用示例。
说明:
如果不适用指定的字体,那么就用imagestring()函数,如果需要遇到指定的字体,就要用到imagettftext()函数。字体的位置在C盘下Windows/Fonts.

参考了网上的php 生成验证码的方法,以及php 图片验证码和php 中文验证码的生成方法。用到了PHP GD库的相关知识。

1,生成验证码的类 VerificationCode.class.php

<?php  
    class VerificationCode{  
        private $charset="abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789";  //随机因子  
        private $code;  //验证码  
        private $codelen=4; //验证码长度  
        private $width=110; //宽度  
        private $height=30; //高度  
        private $img;   //图像资源句柄  
        private $font;  //制定字体  
        private $fontSize=25;   //字体大小  
        private $fontColor; //字体颜色  
        public function __construct(){  
            $this->font="CALIBRIZ.TTF";  
        }  
        //生成验证码  
        private function createCode(){  
            $len=strlen($this->charset)-1;  
            for ($i = 0; $i < $this->codelen; $i++) {  
                $this->code .= $this->charset[mt_rand(0,$len)];  
            }  
        }  
        //生成背景  
        private function createBg(){  
            $this->img=imagecreatetruecolor($this->width,$this->height);  
            $color = imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));  
            imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);  
        }  
        //生成文字  
        private function createFont(){  
            $x=$this->width/$this->codelen;  
            for ($i = 0; $i < $this->codelen; $i++) {  
                $this->fontColor=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));  
                imagettftext($this->img,$this->fontSize,mt_rand(-30,30),$i*$x+mt_rand(1,5),$this->height/1.4,$this->fontColor,$this->font,$this->code[$i]);  // 3water.com
                //imagestring($this->img,5,$i*$x+mt_rand(1,5),5,$this->code[$i],$this->fontColor);  
            }  
        }  
        //生成线条、雪花  
        private function createDisturb(){  
            for ($i = 0; $i < 6; $i++) {  
                $color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));  
                imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->width),mt_rand(0,$this->width),mt_rand(0,$this->width),$color);  
            }  
            for ($i = 0; $i < 100; $i++) {  
                $color=imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));  
                imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);  
            }  
        }  
        //输出  
        private function outPut(){  
            header("Content-Type:image/png");  
            imagepng($this->img);  
            imagedestroy($this->img);  
        }  
        public function showCode(){  
            $this->createBg();  
            $this->createCode();  
            $this->createDisturb();  
            $this->createFont();  
            $this->outPut();  
        }  
        //获取验证码  
        public function getCode(){  
            return strtolower($this->code);  
        }  
    }  
?>

code.php

<?php  
    session_start();  
    require_once 'VerificationCode.class.php';  
    $code=new VerificationCode();  
    $_SESSION['code']=$code->getCode();  
    $code->showCode();  
?>  
验证码:<input type="text" name="code" /><img src="code.php" onclick="javascript:this.src='code.php?time='+Math.random();" />
PHP 相关文章推荐
为php4加入动态flash文件的生成的支持
Oct 09 PHP
十天学会php(2)
Oct 09 PHP
[转帖]PHP世纪万年历
Dec 06 PHP
php session_start()出错原因分析及解决方法
Oct 28 PHP
php实现cc攻击防御和防止快速刷新页面示例
Feb 13 PHP
PHP检测移动设备类mobile detection使用实例
Apr 14 PHP
PHP mkdir()无写权限的问题解决方法
Jun 19 PHP
使用PHP和HTML5 FormData实现无刷新文件上传教程
Sep 06 PHP
php安装php_rar扩展实现rar文件读取和解压的方法
Nov 17 PHP
PHP 二维array转换json的实例讲解
Aug 21 PHP
微信公众平台开发教程⑤ 微信扫码支付模式介绍
Apr 10 PHP
PHP Web表单生成器案例分析
Jun 02 PHP
PHP连接SQLServer2005方法及代码
Dec 26 #PHP
php截取中文字符串不乱码的方法
Dec 25 #PHP
php输入流php://input使用示例(php发送图片流到服务器)
Dec 25 #PHP
php二维数组排序方法(array_multisort usort)
Dec 25 #PHP
php缩小png图片不损失透明色的解决方法
Dec 25 #PHP
php查看请求头信息获取远程图片大小的方法分享
Dec 25 #PHP
php对数组排序的简单实例
Dec 25 #PHP
You might like
php入门学习知识点五 关于php数组的几个基本操作
2011/07/14 PHP
PHP mysql与mysqli事务使用说明 分享
2013/08/17 PHP
Laravel中错误与异常处理的用法示例
2018/09/16 PHP
详解Laravel设置多态关系模型别名的方式
2019/10/17 PHP
php实现通过stomp协议连接ActiveMQ操作示例
2020/02/23 PHP
Some tips of wmi scripting in jscript (1)
2007/04/03 Javascript
jquery ajax提交表单数据的两种实现方法
2010/04/29 Javascript
jquery的Tooltip插件 qtip使用详细说明
2010/09/08 Javascript
JS实现在Repeater控件中创建可隐藏区域的代码
2010/09/16 Javascript
Jquery UI震动效果实现原理及步骤
2013/02/04 Javascript
jQuery性能优化28条建议你值得借鉴
2013/02/16 Javascript
javaScript中的this示例学习详解及工作原理
2014/01/13 Javascript
Javascript字符串对象的常用方法简明版
2014/06/26 Javascript
在Linux上用forever实现Node.js项目自启动
2014/07/09 Javascript
在JavaScript中操作时间之setYear()方法的使用
2015/06/12 Javascript
jQuery插件支持同一页面被多次调用
2016/02/14 Javascript
js实现异步循环实现代码
2016/02/16 Javascript
AngularJS学习笔记之依赖注入详解
2016/05/16 Javascript
jQuery包裹节点用法完整示例
2016/09/13 Javascript
js设置随机切换背景图片的简单实例
2017/11/12 Javascript
一篇文章弄懂javascript中的执行栈与执行上下文
2019/08/09 Javascript
解决layer.msg 不居中 ifram中的问题
2019/09/05 Javascript
Vue实现todo应用的示例
2021/02/20 Vue.js
[01:32:10]NAVI vs VG Supermajor 败者组 BO3 第一场 6.5
2018/06/06 DOTA
python3+django2开发一个简单的人员管理系统过程详解
2019/07/23 Python
Pandas之groupby( )用法笔记小结
2019/07/23 Python
python生成器/yield协程/gevent写简单的图片下载器功能示例
2019/10/28 Python
python中的subprocess.Popen()使用详解
2019/12/25 Python
GDAL 矢量属性数据修改方式(python)
2020/03/10 Python
Python多线程正确用法实例解析
2020/05/30 Python
Python使用xpath实现图片爬取
2020/09/16 Python
室内设计专业个人的自我评价
2013/10/19 职场文书
公务员试用期满考核材料
2014/05/22 职场文书
物流管理系毕业生求职信
2014/06/03 职场文书
公司回复函格式
2015/07/14 职场文书
电力企业职工培训心得体会
2016/01/11 职场文书