一个好用的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来自动调用不同服务器上的flash
Oct 09 PHP
Ajax PHP分页演示
Jan 02 PHP
Windows IIS PHP 5.2 安装与配置方法
Jun 08 PHP
七款最流行的PHP本地服务器分享
Feb 19 PHP
解析php中eclipse 用空格替换 tab键
Jun 24 PHP
thinkphp实现图片上传功能分享
Mar 04 PHP
五款PHP代码重构工具推荐
Oct 14 PHP
我整理的PHP 7.0主要新特性
Jan 07 PHP
学习PHP的数组总结【经验】
May 05 PHP
Yii核心验证器api详解
Nov 23 PHP
详细解读php的命名空间(一)
Feb 21 PHP
PHP生成随机密码4种方法及性能对比
Dec 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
thinkPHP框架整合tcpdf插件操作示例
2018/08/07 PHP
PHP数据对象映射模式实例分析
2019/03/29 PHP
一个JavaScript继承的实现
2006/10/24 Javascript
js获取单元格自定义属性值的代码(IE/Firefox)
2010/04/05 Javascript
JavaScript 浏览器验证代码(来自discuz)
2010/07/17 Javascript
javascript 学习笔记(onchange等)
2010/11/14 Javascript
基于JQUERY的两个ListBox子项互相调整的实现代码
2011/05/07 Javascript
JS的get和set使用示例
2014/02/20 Javascript
jQuery中prop()方法用法实例
2015/01/05 Javascript
JS实现六位字符密码输入器功能
2016/08/19 Javascript
getElementById().innerHTML与getElementById().value的区别
2016/10/27 Javascript
JS高级运动实例分析
2016/12/20 Javascript
微信小程序 http请求的session管理
2017/06/07 Javascript
vue2.0设置proxyTable使用axios进行跨域请求的方法
2017/10/19 Javascript
详解angular应用容器化部署
2018/08/14 Javascript
详解微信小程序canvas圆角矩形的绘制的方法
2018/08/22 Javascript
微信小程序用canvas画图并分享
2020/03/09 Javascript
简明 Python 基础学习教程
2007/02/08 Python
python提示No module named images的解决方法
2014/09/29 Python
Python矩阵常见运算操作实例总结
2017/09/29 Python
python中时间、日期、时间戳的转换的实现方法
2019/07/06 Python
微信公众号token验证失败解决方案
2019/07/22 Python
Python爬虫爬取煎蛋网图片代码实例
2019/12/16 Python
Python 字符串处理特殊空格\xc2\xa0\t\n Non-breaking space
2020/02/23 Python
python 基于wx实现音乐播放
2020/11/24 Python
虚拟环境及venv和virtualenv的区别说明
2021/02/05 Python
HTML5超文本标记语言的实现方法
2020/09/24 HTML / CSS
法国一家芭蕾舞鞋公司:Repetto
2018/11/12 全球购物
奥巴马演讲稿
2014/01/08 职场文书
小学生母亲节演讲稿
2014/05/07 职场文书
文艺演出策划方案
2014/06/07 职场文书
物理分数没达标检讨书
2014/09/13 职场文书
《浅水洼里的小鱼》教学反思
2016/02/16 职场文书
导游词之云南丽江-泸沽湖
2019/09/26 职场文书
如何在Python中创建二叉树
2021/03/30 Python
聊聊CSS粘性定位sticky案例解析
2022/06/01 HTML / CSS