一个好用的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中判断一个字符串包含另一个字符串的方法
Mar 19 PHP
如何使用Strace调试工具
Jun 03 PHP
PHP编程风格规范分享
Jan 15 PHP
使用CodeIgniter的类库做图片上传
Jun 12 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十一)
Jun 25 PHP
php中Socket创建与监听实现方法
Jan 05 PHP
php中通过DirectoryIterator删除整个目录的方法
Mar 13 PHP
浅谈PHP中foreach/in_array的使用
Nov 02 PHP
讲解WordPress中用于获取评论模板和搜索表单的PHP函数
Dec 28 PHP
PHP实现数组转JSon和JSon转数组的方法示例
Jun 14 PHP
php中青蛙跳台阶的问题解决方法
Oct 14 PHP
微信小程序和php的登录实现
Apr 01 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面向对象学习笔记之二 生成对象的设计模式
2012/10/06 PHP
php全排列递归算法代码
2012/10/09 PHP
php一次性删除前台checkbox多选内容的方法
2013/09/22 PHP
php define的第二个参数使用方法
2013/11/04 PHP
WordPress中登陆后关闭登陆页面及设置用户不可见栏目
2015/12/31 PHP
PHP实现简单用户登录界面
2019/10/23 PHP
JQuery Tips(3) 关于$()包装集内元素的改变
2009/12/14 Javascript
如何确保JavaScript的执行顺序 之jQuery.html并非万能钥匙
2011/03/03 Javascript
Javascript 页面模板化很多人没有使用过的方法
2012/06/05 Javascript
js倒计时抢购实例
2015/12/20 Javascript
AngularJS表单提交实例详解
2017/02/18 Javascript
jquery实现页面加载效果
2017/02/21 Javascript
微信小程序表单验证错误提示效果
2017/05/19 Javascript
二维码图片生成器QRCode.js简单介绍
2017/08/18 Javascript
Vue添加请求拦截器及vue-resource 拦截器使用
2017/11/23 Javascript
深入理解js 中async 函数的含义和用法
2018/05/13 Javascript
Vue 中axios配置实例详解
2018/07/27 Javascript
jquery实现动态添加附件功能
2018/10/23 jQuery
Vuex的基本概念、项目搭建以及入坑点
2018/11/04 Javascript
详解三种方式解决vue中v-html元素中标签样式
2018/11/22 Javascript
JavaScript显式数据类型转换详解
2019/03/18 Javascript
vue.js+ElementUI实现进度条提示密码强度效果
2020/01/18 Javascript
一篇文章带你使用Typescript封装一个Vue组件(简单易懂)
2020/06/05 Javascript
Angular+ionic实现折叠展开效果的示例代码
2020/07/29 Javascript
解决vant框架做H5时踩过的坑(下拉刷新、上拉加载等)
2020/11/11 Javascript
[03:57]2016完美“圣”典风云人物:rOtk专访
2016/12/09 DOTA
python3实现短网址和数字相互转换的方法
2015/04/28 Python
Python使用filetype精确判断文件类型
2017/07/02 Python
详解Python修复遥感影像条带的两种方式
2020/02/23 Python
在主流系统之上安装Pygame的方法
2020/05/20 Python
创业计划实施的7大步骤
2014/02/05 职场文书
房地产营销策划方案
2014/02/08 职场文书
2016年“我们的节日·端午节”活动总结
2016/04/01 职场文书
英语版自我评价,35句话轻松搞定
2019/10/08 职场文书
详细聊聊vue中组件的props属性
2021/11/02 Vue.js
Mysql数据库表中为什么有索引却没有提高查询速度
2022/02/24 MySQL