一个好用的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 相关文章推荐
网页游戏开发入门教程三(简单程序应用)
Nov 02 PHP
PHP 文本文章分页代码 按标记或长度(不涉及数据库)
Jun 07 PHP
改写函数实现PHP二维/三维数组转字符串
Sep 13 PHP
php实现根据url自动生成缩略图的方法
Sep 23 PHP
windows8.1下Apache+Php+MySQL配置步骤
Oct 30 PHP
php简单实现sql防注入的方法
Apr 22 PHP
php获取网站根目录物理路径的几种方法(推荐)
Mar 04 PHP
PHP使用第三方即时获取物流动态实例详解
Apr 27 PHP
PHP回调函数概念与用法实例分析
Nov 03 PHP
php app支付宝回调(异步通知)详解
Jul 25 PHP
Laravel框架自定义公共函数的引入操作示例
Apr 16 PHP
Yii框架Session与Cookie使用方法示例
Oct 14 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
Windows7下PHP开发环境安装配置图文方法
2010/05/20 PHP
thinkphp3.x自定义Action、Model及View的简单实现方法
2016/05/19 PHP
php+jQuery递归调用POST循环请求示例
2016/10/14 PHP
php数据库的增删改查 php与javascript之间的交互
2017/08/31 PHP
js右键菜单效果代码
2007/07/21 Javascript
JQuery Ajax通过Handler访问外部XML数据的代码
2010/06/01 Javascript
jquery.boxy插件的iframe扩展代码
2010/07/02 Javascript
用显卡加速,轻松把笔记本打造成取暖器的办法!
2013/04/17 Javascript
jquery中attr和prop的区别分析
2015/03/16 Javascript
基于jQuery实现的旋转彩圈实例
2015/06/26 Javascript
javascript回到顶部特效
2016/07/30 Javascript
Angularjs中$http以post请求通过消息体传递参数的实现方法
2016/08/05 Javascript
最丑的时钟效果!js canvas时钟制作方法
2016/08/15 Javascript
Javascript json object 与string 相互转换的简单实现
2016/09/27 Javascript
使用BootStrap实现悬浮窗口的效果
2016/12/13 Javascript
详解JavaScript调用栈、尾递归和手动优化
2017/06/03 Javascript
Angularjs 手写日历的实现代码(不用插件)
2017/10/18 Javascript
vue项目中api接口管理总结
2018/04/20 Javascript
在Vant的基础上实现添加表单验证框架的方法示例
2018/12/05 Javascript
Django+vue跨域问题解决的详细步骤
2019/01/20 Javascript
Node.js原生api搭建web服务器的方法步骤
2019/02/15 Javascript
javascript设计模式之迭代器模式
2020/01/30 Javascript
[00:06]Yes,it worked!小卡尔成功穿越时空加入战场!
2019/07/20 DOTA
Python的Django框架中的select_related函数对QuerySet 查询的优化
2015/04/01 Python
python实现linux下使用xcopy的方法
2015/06/28 Python
Python3连接SQLServer、Oracle、MySql的方法
2018/06/28 Python
python 3调用百度OCR API实现剪贴板文字识别
2018/09/04 Python
更新修改后的Python模块方法
2019/03/03 Python
Tory Burch德国官网:美国时尚生活品牌
2018/01/03 全球购物
J2EE的优越性主要表现在哪些方面
2016/03/28 面试题
工商干部先进事迹
2014/05/14 职场文书
法律系毕业生求职信
2014/05/28 职场文书
委托证明模板
2014/09/16 职场文书
介绍信应该怎么开?
2019/04/03 职场文书
浅谈MySQL之select优化方案
2021/08/07 MySQL
搭建zabbix监控以及邮件报警的超级详细教学
2022/07/15 Servers