一个好用的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 高级课程笔记 面向对象
Jun 21 PHP
php 使用post,get的一种简洁方式
Apr 25 PHP
PHP中图片等比缩放的实例
Mar 24 PHP
php unset全局变量运用问题的深入解析
Jun 17 PHP
php实现图片添加水印功能
Feb 13 PHP
详解WordPress中分类函数wp_list_categories的使用
Jan 04 PHP
php读取出一个文件夹及其子文件夹下所有文件的方法示例
Jun 15 PHP
php实现的错误处理封装类实例
Jun 20 PHP
PHP自定义函数判断是否为Get、Post及Ajax提交的方法
Jul 27 PHP
PHP简单实现二维数组的矩阵转置操作示例
Nov 24 PHP
Laravel使用RabbitMQ的方法示例
Jun 18 PHP
laravel 获取当前url的别名方法
Oct 11 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 创建标签云函数代码
2010/05/26 PHP
php实现文件下载功能的几个代码分享
2014/05/10 PHP
php相对当前文件include其它文件的方法
2015/03/13 PHP
PHP结合jQuery实现找回密码
2015/07/22 PHP
JavaScript中的私有成员
2006/09/18 Javascript
ImageFlow可鼠标控制图片滚动
2008/01/30 Javascript
javascript或asp实现的判断身份证号码是否正确两种验证方法
2009/11/26 Javascript
js实现网页标题栏闪烁提示效果实例分析
2014/11/20 Javascript
jquery 插件实现多行文本框[textarea]自动高度
2015/03/04 Javascript
JavaScript 对象深入学习总结(经典)
2015/09/29 Javascript
JS去掉字符串末尾的标点符号及删除最后一个字符的方法
2017/10/24 Javascript
vue.js 实现点击展开收起动画效果
2018/07/07 Javascript
JavaScript实现异步图像上传功能
2018/07/12 Javascript
vue动态删除从数据库倒入列表的某一条方法
2018/09/29 Javascript
jQuery轮播图功能制作方法详解
2019/12/03 jQuery
javascript设计模式 ? 访问者模式原理与用法实例分析
2020/04/26 Javascript
Vue使用Element实现增删改查+打包的步骤
2020/11/25 Vue.js
[05:09]第二届DOTA2亚洲邀请赛决赛日比赛集锦:iG 3:0 OG夺冠
2017/04/05 DOTA
[01:35]2018完美盛典章节片——共竞
2018/12/17 DOTA
Python解析最简单的验证码
2016/01/07 Python
python spyder中读取txt为图片的方法
2018/04/27 Python
Python 实现try重新执行
2019/12/21 Python
详解HTML5中的拖放事件(Drag 和 drop)
2016/11/14 HTML / CSS
英国广泛的照明产品网站:Lights4living
2018/01/28 全球购物
Fenty Beauty官网:蕾哈娜创立的美妆品牌
2021/01/07 全球购物
给医务人员表扬信
2014/01/12 职场文书
给幼儿园老师的表扬信
2014/01/19 职场文书
还款承诺书范文
2014/05/20 职场文书
论文诚信承诺书
2014/05/23 职场文书
党支部对转正的意见
2015/06/02 职场文书
MySQL入门命令之函数-单行函数-流程控制函数
2021/04/05 MySQL
python中requests库+xpath+lxml简单使用
2021/04/29 Python
Python数据分析入门之数据读取与存储
2021/05/13 Python
MySQL表类型 存储引擎 的选择
2021/11/11 MySQL
详解MySQL中timestamp和datetime时区问题导致做DTS遇到的坑
2021/12/06 MySQL
Java线程的6种状态与生命周期
2022/05/11 Java/Android