一个好用的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 相关文章推荐
2.PHP入门
Oct 09 PHP
php 防止单引号,双引号在接受页面转义
Jul 10 PHP
一个php导出oracle库的php代码
Apr 20 PHP
在IIS7.0下面配置PHP 5.3.2运行环境的方法
Apr 13 PHP
php去除换行符的方法小结(PHP_EOL变量的使用)
Feb 16 PHP
探讨:parse url解析URL,返回其组成部分
Jun 14 PHP
在SAE上搭建最新wordpress的方法
Dec 21 PHP
PHP 使用 Imagick 裁切/生成缩略图/添加水印自动检测和处理 GIF
Feb 19 PHP
全面解析PHP验证码的实现原理 附php验证码小案例
Aug 17 PHP
PHP基于socket实现的简单客户端和服务端通讯功能示例
Jul 10 PHP
详解PHP文件的自动加载(autoloading)
Feb 04 PHP
thinkPHP框架通过Redis实现增删改查操作的方法详解
May 13 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/03/29 PHP
JMenuTab简单使用说明
2008/03/13 Javascript
fireworks菜单生成器mm_menu.js在 IE 7.0 显示问题的解决方法
2009/10/20 Javascript
window.js 主要包含了页面的一些操作
2009/12/23 Javascript
jQuery Ajax请求状态管理器打包
2012/05/03 Javascript
使用js操作css实现js改变背景图片示例
2014/03/10 Javascript
常用的jQuery前端技巧收集
2014/12/24 Javascript
jQuery实现输入框下拉列表树插件特效代码分享
2015/08/27 Javascript
[原创]javascript typeof id==='string'?document.getElementById(id):id解释
2016/11/02 Javascript
ionic+AngularJs实现获取验证码倒计时按钮
2017/04/22 Javascript
jQuery手风琴的简单制作
2017/05/12 jQuery
微信小程序 侧滑删除(左滑删除)
2017/05/23 Javascript
基于JavaScript实现幸运抽奖页面
2020/07/05 Javascript
vue页面更新patch的实现示例
2020/03/25 Javascript
elementui实现预览图片组件二次封装
2020/12/29 Javascript
使用jquery实现轮播图效果
2021/01/02 jQuery
Python 文件重命名工具代码
2009/07/26 Python
Python中列表(list)操作方法汇总
2014/08/18 Python
Python实现的检测web服务器健康状况的小程序
2014/09/17 Python
Python中list初始化方法示例
2016/09/18 Python
Python实现购物车功能的方法分析
2017/11/10 Python
详解从Django Rest Framework响应中删除空字段
2019/01/11 Python
Python3进制之间的转换代码实例
2019/08/24 Python
Python解释器以及PyCharm的安装教程图文详解
2020/02/26 Python
解决python 执行sql语句时所传参数含有单引号的问题
2020/06/06 Python
Python列表推导式实现代码实例
2020/09/09 Python
基于python实现简单C/S模式代码实例
2020/09/14 Python
python3爬虫中引用Queue的实例讲解
2020/11/24 Python
python subprocess pipe 实时输出日志的操作
2020/12/05 Python
python3.9和pycharm的安装教程并创建简单项目的步骤
2021/02/03 Python
css3 实现元素弧线运动的示例代码
2020/04/24 HTML / CSS
Asics日本官网:鬼冢八喜郎创立的跑鞋运动品牌
2017/10/18 全球购物
世界上最大的皮肤科医生拥有和经营的美容网站:LovelySkin
2021/01/03 全球购物
个人简历自我评价八例
2013/10/31 职场文书
党的群众路线教育实践活动个人整改措施
2014/10/27 职场文书
2015年基层党支部工作总结
2015/05/21 职场文书