一个好用的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 相关文章推荐
Linux下ZendOptimizer的安装与配置方法
Apr 12 PHP
PHP获取当前页面完整URL的实现代码
Jun 10 PHP
php教程之phpize使用方法
Feb 12 PHP
PHP防止post重复提交数据的简单例子
Jun 07 PHP
PHP实现服务器状态监控的方法
Dec 09 PHP
网页的分页下标生成代码(PHP后端方法)
Feb 03 PHP
深入解析Laravel5.5中的包自动发现Package Auto Discovery
Sep 13 PHP
PHP 访问数据库配置通用方法(json)
May 20 PHP
分享5个非常有用的Laravel Blade指令
May 30 PHP
PHP中soap用法示例【SoapServer服务端与SoapClient客户端编写】
Dec 25 PHP
PHP商品秒杀问题解决方案实例详解【mysql与redis】
Jul 22 PHP
解决windows上php xdebug 无法调试的问题
Feb 19 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的分页功能
2007/03/21 PHP
从手册去理解分析PHP session机制
2011/07/17 PHP
将php数组输出html表格的方法
2014/02/24 PHP
在laravel中实现ORM模型使用第二个数据库设置
2019/10/24 PHP
根据对象的某一属性进行排序的js代码(如:name,age)
2010/08/10 Javascript
jquery 与NVelocity 产生冲突的解决方法
2011/06/13 Javascript
jQuery异步加载数据并添加事件示例
2014/08/24 Javascript
javascript面向对象之定义成员方法实例分析
2015/01/13 Javascript
Jquery实现仿京东商城省市联动菜单
2015/11/19 Javascript
基于javascript实现图片滑动效果
2016/05/07 Javascript
微信小程序 wxapp内容组件 text详细介绍
2016/10/31 Javascript
Bootstrap的modal拖动效果
2016/12/25 Javascript
jQuery动态生成表格及右键菜单功能示例
2017/01/13 Javascript
nodejs mysql 实现分页的方法
2017/06/06 NodeJs
javascript基础进阶_深入剖析执行环境及作用域链
2017/09/05 Javascript
vue-ajax小封装实例
2017/09/18 Javascript
基于jQuery选择器之表单对象属性筛选选择器的实例
2017/09/19 jQuery
vue获取当前激活路由的方法
2018/03/17 Javascript
JS实现纵向轮播图(初级版)
2020/01/18 Javascript
Preload基础使用方法详解
2020/02/03 Javascript
基于Element的组件改造的树形选择器(树形下拉框)
2020/02/27 Javascript
JavaScript函数重载操作实例浅析
2020/05/02 Javascript
Vue点击切换Class变化,实现Active当前样式操作
2020/07/17 Javascript
前端开发基础javaScript的六大作用
2020/08/06 Javascript
Python的Flask框架及Nginx实现静态文件访问限制功能
2016/06/27 Python
Python 读取用户指令和格式化打印实现解析
2019/09/02 Python
python3 pillow模块实现简单验证码
2019/10/31 Python
Python实现打包成库供别的模块调用
2020/07/13 Python
关于python3.7安装matplotlib始终无法成功的问题的解决
2020/07/28 Python
CSS3实现闪烁动画效果的方法
2015/02/09 HTML / CSS
北京RT科技有限公司.net工程师面试题
2013/02/15 面试题
计算机专业职业规划
2014/02/28 职场文书
安全月活动总结
2014/05/05 职场文书
基本公共卫生服务健康教育工作方案
2014/05/22 职场文书
妇产科护理心得体会
2016/01/22 职场文书
教师实习自我鉴定总结
2019/08/20 职场文书