一个好用的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 相关文章推荐
模仿OSO的论坛(一)
Oct 09 PHP
深入掌握include_once与require_once的区别
Jun 17 PHP
php对二维数组按指定键值key排序示例代码
Nov 26 PHP
ThinkPHP3.1新特性之查询条件预处理简介
Jun 19 PHP
thinkphp模板赋值与替换实例简述
Nov 24 PHP
php实现四舍五入的方法小结
Mar 03 PHP
php字符串分割函数用法实例
Mar 17 PHP
PHP实现事件机制实例分析
Jun 26 PHP
php生成gif动画的方法
Nov 05 PHP
PHP后台实现微信小程序登录
Aug 03 PHP
php设计模式之观察者模式定义与用法经典示例
Sep 19 PHP
PHP 时间处理类Carbon
May 20 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
《破坏领主》销量已超100万 未来将继续开发新内容
2020/03/08 其他游戏
一个PHP的QRcode类与大家分享
2011/11/13 PHP
php实现的mongodb操作类实例
2015/04/03 PHP
Firefox window.close()的使用注意事项
2009/04/11 Javascript
jQuery获取css z-index在各种浏览器中的返回值
2010/09/15 Javascript
js中widow.open()方法使用详解
2013/07/30 Javascript
JavaScript避免内存泄露及内存管理技巧
2014/09/05 Javascript
根据配置文件加载js依赖模块
2014/12/29 Javascript
jQuery中:only-child选择器用法实例
2015/01/03 Javascript
BootStrap 动态添加验证项和取消验证项的实现方法
2016/09/28 Javascript
简单实现jQuery多选框功能
2017/01/09 Javascript
jQuery插件FusionCharts绘制ScrollColumn2D图效果示例【附demo源码下载】
2017/03/22 jQuery
jQuery查找和过滤_动力节点节点Java学院整理
2017/07/04 jQuery
js 概率计算(简单版)
2017/09/12 Javascript
浅谈VUE监听窗口变化事件的问题
2018/02/24 Javascript
创建echart多个联动的示例代码
2018/11/23 Javascript
浅谈Vuex注入Vue生命周期的过程
2019/05/20 Javascript
node.js中事件触发器events的使用方法实例分析
2019/11/23 Javascript
[03:49]2016完美“圣”典风云人物:AMS专访
2016/12/06 DOTA
Python原始字符串(raw strings)用法实例
2014/10/13 Python
Python批量修改文本文件内容的方法
2016/04/29 Python
Python 网络编程之TCP客户端/服务端功能示例【基于socket套接字】
2019/10/12 Python
opencv3/python 鼠标响应操作详解
2019/12/11 Python
PyCharm如何导入python项目的方法
2020/02/06 Python
Python如何读取、写入JSON数据
2020/07/28 Python
python 实现的车牌识别项目
2021/01/25 Python
C#面试常见问题
2013/02/25 面试题
Shell编程面试题
2016/05/29 面试题
有针对性的求职自荐信
2013/11/14 职场文书
注塑工厂厂长岗位职责
2013/12/02 职场文书
银行工作检查书范文
2014/01/31 职场文书
民族团结先进个人事迹材料
2014/06/02 职场文书
2015年圣诞节活动总结
2015/03/24 职场文书
房屋所有权证明
2015/06/19 职场文书
导游词之麻姑仙境
2019/11/18 职场文书
vue基于Teleport实现Modal组件
2021/05/31 Vue.js