一个好用的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 相关文章推荐
强烈推荐:php.ini中文版(2)
Oct 09 PHP
php中将地址生成迅雷快车旋风链接的代码[测试通过]
Apr 20 PHP
说说PHP的autoLoad自动加载机制
Sep 27 PHP
php 删除目录下N分钟前创建的所有文件的实现代码
Aug 10 PHP
php inc文件使用的风险和注意事项
Nov 12 PHP
PHP生成自适应大小的缩略图类及使用方法分享
May 06 PHP
php数组分页实现方法
Apr 30 PHP
PHP时间戳格式全部汇总 (获取时间、时间戳)
Jun 13 PHP
thinkPHP中配置的读取与C方法详解
Dec 05 PHP
快速解决PHP调用Word组件DCOM权限的问题
Dec 27 PHP
PHP学习笔记之session
May 06 PHP
YII2框架中查询生成器Query()的使用方法示例
Mar 18 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
Zend公司全球首推PHP认证
2006/10/09 PHP
Ajax+PHP 边学边练之四 表单
2009/11/27 PHP
利用curl抓取远程页面内容的示例代码
2013/07/23 PHP
php similar_text()函数的定义和用法
2016/05/12 PHP
php基于闭包实现函数的自调用(递归)实例分析
2016/11/11 PHP
PHP三种方式实现链式操作详解
2017/01/21 PHP
Laravel 5.5 的自定义验证对象/类示例代码详解
2017/08/29 PHP
javascript 有趣而诡异的数组
2009/04/06 Javascript
jQuery Ajax之load()方法
2009/10/12 Javascript
SlideView 图片滑动(扩展/收缩)展示效果
2010/08/01 Javascript
js Dialog 实践分享
2012/10/22 Javascript
JS定时器实例详细分析
2013/10/11 Javascript
jQuery中:input选择器用法实例
2015/01/03 Javascript
CSS javascript 结合实现悬浮固定菜单效果
2015/08/23 Javascript
基于jquery实现智能表单验证操作
2016/05/09 Javascript
AngularJS在IE8的不支持的解决方法
2016/05/13 Javascript
基于jquery插件编写countdown计时器
2016/06/12 Javascript
原生js实现密码输入框值的显示隐藏
2017/07/17 Javascript
Windows下使用Nodejs运行js的方法
2017/09/02 NodeJs
微信小程序实现图片压缩功能
2018/01/26 Javascript
python中的一些类型转换函数小结
2013/02/10 Python
centos 下面安装python2.7 +pip +mysqld
2014/11/18 Python
python实现感知器算法(批处理)
2019/01/18 Python
pytorch中的transforms模块实例详解
2019/12/31 Python
python 将dicom图片转换成jpg图片的实例
2020/01/13 Python
关于keras.layers.Conv1D的kernel_size参数使用介绍
2020/05/22 Python
Python过滤序列元素的方法
2020/07/31 Python
澳大利亚相机之家:Camera House
2017/11/30 全球购物
印尼第一大家居、生活和家具电子商务:Ruparupa
2019/11/25 全球购物
The Outnet亚太地区:折扣设计师时装店
2019/12/05 全球购物
九年级政治教学反思
2014/02/06 职场文书
户籍证明模板
2014/09/28 职场文书
2015年小学远程教育工作总结
2015/07/28 职场文书
长辈生日祝福语大全(72句)
2019/08/09 职场文书
Apache Linkis 中间件架构及快速安装步骤
2022/03/16 Servers
JS前端使用canvas实现物体的点选示例
2022/08/05 Javascript