一个好用的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对gzip文件或者字符串解压实例参考
Jul 25 PHP
php MsSql server时遇到的中文编码问题
Jun 11 PHP
phpMyAdmin链接MySql错误 个人解决方案
Dec 28 PHP
PHP 调试工具Debug Tools
Apr 30 PHP
解析PHP可变函数的经典用法
Jun 20 PHP
Discuz7.2版的faq.php SQL注入漏洞分析
Aug 06 PHP
PHP模板引擎Smarty内建函数详解
Apr 11 PHP
php项目开发中用到的快速排序算法分析
Jun 25 PHP
利用 fsockopen() 函数开放端口扫描器的实例
Aug 19 PHP
laravel 模型查询按照whereIn排序的示例
Oct 16 PHP
php中try catch捕获异常实例详解
Aug 06 PHP
PHP扩展安装方法步骤解析
Nov 24 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
弹出模态框modal的实现方法及实例
2017/09/19 PHP
php查看一个变量的占用内存的实例代码
2020/03/29 PHP
JavaScript高级程序设计
2006/12/29 Javascript
JavaScript中的类继承
2010/11/25 Javascript
JavaScript实现页面实时显示当前时间的简单实例
2013/07/20 Javascript
jquery实现可拖拽弹出层特效
2015/01/04 Javascript
jQuery中:disabled选择器用法实例
2015/01/04 Javascript
JavaScript使用yield模拟多线程的方法
2015/03/19 Javascript
创建自己的jquery表格插件
2015/11/25 Javascript
EasyUI布局 高度自适应
2016/06/04 Javascript
jQuery中Find选择器用法示例
2016/09/21 Javascript
js实现下拉菜单效果
2017/03/01 Javascript
你点的 ES6一些小技巧,请查收
2018/04/25 Javascript
学习Vue组件实例
2018/04/28 Javascript
解决vue.js 数据渲染成功仍报错的问题
2018/08/25 Javascript
微信小程序定义和调用全局变量globalData的实现
2019/11/01 Javascript
vuex实现像调用模板方法一样调用Mutations方法
2019/11/06 Javascript
Vue搭建后台系统需要注意的问题
2019/11/08 Javascript
vue列表数据发生变化指令没有更新问题及解决方法
2020/01/16 Javascript
jQuery实现鼠标滑动切换图片
2020/05/27 jQuery
[02:37]2018DOTA2亚洲邀请赛赛前采访 VP.no[o]ne心中最强SOLO是谁
2018/04/04 DOTA
使用Python的Django框架实现事务交易管理的教程
2015/04/20 Python
python定时按日期备份MySQL数据并压缩
2019/04/19 Python
在python中做正态性检验示例
2019/12/09 Python
pytorch构建多模型实例
2020/01/15 Python
pytorch实现MNIST手写体识别
2020/02/14 Python
Mavi牛仔裤美国官网:土耳其著名牛仔品牌
2016/09/24 全球购物
Shopee越南:东南亚与台湾电商平台
2019/02/03 全球购物
应届生求职信写作技巧
2013/10/24 职场文书
民主评议党员自我评议范文2014
2014/09/26 职场文书
2014年保卫科工作总结
2014/12/05 职场文书
2015年五四青年节演讲稿
2015/03/18 职场文书
2015年社区矫正工作总结
2015/04/21 职场文书
法律意见书范文
2015/06/04 职场文书
Nginx开启Brotli压缩算法实现过程详解
2021/03/31 Servers
golang 实用库gotable的具体使用
2021/07/01 Golang