一个好用的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个人网站架设连环讲(四)
Oct 09 PHP
用php和MySql来与ODBC数据连接
Oct 09 PHP
php全局变量和类配合使用深刻理解
Jun 05 PHP
thinkphp中session和cookie无效的解决方法
Dec 19 PHP
php实现将上传word文件转为html的方法
Jun 03 PHP
PHP实现过滤掉非汉字字符只保留中文字符
Jun 04 PHP
PHP实现HTTP断点续传的方法
Jun 17 PHP
CentOS 7.2 下编译安装PHP7.0.10+MySQL5.7.14+Nginx1.10.1的方法详解(mini版本)
Sep 01 PHP
Thinkphp框架中D方法与M方法的区别
Dec 23 PHP
php实现连接access数据库并转txt写入的方法
Feb 08 PHP
PHP自定义函数实现assign()数组分配到模板及extract()变量分配到模板功能示例
May 23 PHP
PHP配置ZendOpcache插件加速
Feb 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
DEDE采集大师官方留后门的删除办法
2011/01/08 PHP
PHP实现模仿socket请求返回页面的方法
2014/11/04 PHP
PHP中使用BigMap实例
2015/03/30 PHP
laravel实现按时间日期进行分组统计方法示例
2019/03/23 PHP
javascript中的对象创建 实例附注释
2011/02/08 Javascript
extJS中常用的4种Ajax异步提交方式
2014/03/07 Javascript
nodejs中实现路由功能
2014/12/29 NodeJs
jQuery中extend()和fn.extend()方法详解
2015/06/03 Javascript
JavaScript截断字符串的方法
2015/07/15 Javascript
jquery模拟多级复选框效果的简单实例
2016/06/08 Javascript
jQuery插件passwordStrength密码强度指标详解
2016/06/24 Javascript
jQuery插件Easyui设置datagrid的pageNumber导致两次请求问题的解决方法
2016/08/06 Javascript
js 获取当前web应用的上下文路径实现方法
2016/08/19 Javascript
angular中的http拦截器Interceptors的实现
2017/02/21 Javascript
nodejs简单实现TCP服务器端和客户端的聊天功能示例
2018/01/04 NodeJs
vue实现微信分享朋友圈,发送朋友的示例讲解
2018/02/10 Javascript
使用Jenkins部署React项目的方法步骤
2019/03/11 Javascript
echarts大屏字体自适应的方法步骤
2019/07/12 Javascript
Vue + Element UI图片上传控件使用详解
2019/08/20 Javascript
JS删除数组指定值常用方法详解
2020/06/04 Javascript
Ant design vue table 单击行选中 勾选checkbox教程
2020/10/24 Javascript
Tensorflow中使用tfrecord方式读取数据的方法
2018/06/19 Python
对python numpy.array插入一行或一列的方法详解
2019/01/29 Python
python实现电子书翻页小程序
2019/07/23 Python
tensorflow实现测试时读取任意指定的check point的网络参数
2020/01/21 Python
python爬虫学习笔记之pyquery模块基本用法详解
2020/04/09 Python
网站性能延迟加载图像的五种技巧(小结)
2020/08/13 HTML / CSS
我读书我快乐演讲稿
2014/05/07 职场文书
新闻工作者先进事迹
2014/05/26 职场文书
2014年护理部工作总结
2014/11/14 职场文书
2015暑假假期总结
2015/07/13 职场文书
一年级下册数学教学反思
2016/02/16 职场文书
css position fixed 左右双定位的实现代码
2021/04/29 HTML / CSS
用几道面试题来看JavaScript执行机制
2021/04/30 Javascript
MySQL update set 和 and的区别
2021/05/08 MySQL
html输入两个数实现加减乘除功能
2021/07/01 HTML / CSS