细说php中的验证码类创建

我这里自己写了一个验证码类,我来演示一下怎么使用,我是菜鸟一枚,大神请略过。我来讲解一下它的使用方法,总共需要两步即可。

第一步:

下载我制作好的验证码类。下载地址:http://files.cnblogs.com/files/xfjpeter/Verify.zip

第二步:

 1.创建一个字的验证码文件

 1 <?php
 2
 3 #引入验证码类文件
 4 require_once('Verify.class.php');
 5
 6 #实例化验证码类
 7 #初始化的使用可以传四个参数,分别是:验证码图片的长、高,验证码的长度,验证码的类型(验证码的类型需要将bgRand属性设置为false)
 8 $code = new Verify(140, 40, 6, 6);
 9
10 #设置验证码图片的长度
11 $code -> width = 200;
12
13 #设置验证码图片的高度
14 $code -> height = 60;
15
16 #是否随机背景,默认true(随机)
17 $code -> bgRand = false;
18
19 #显示验证码
20 $code -> verify();

 生成的图片样式为如图

2.验证码类文件为

  1 <?php
  2
  3 /**
  4  * 验证码类
  5  * @author John <fsyzxz@163.com>
  6  */
  7 class Verify
  8 {
  9     private $width = 160;       //验证码的宽度
 10     private $height = 60;       //验证码的高度
 11     private $type = 1;          //验证码的类型
 12     private $length = 4;        //验证码的长度
 13     private $code;              //验证码
 14     private $img;               //图像的资源
 15     private $seKey = 'John';    //密钥
 16     private $bgRand = true;     //随机背景图片
 17
 18     /**
 19      * 构造函数
 20      * @param type $width 验证码的宽度
 21      * @param type $height 验证码的高度
 22      * @param type $length 验证码的长度
 23      * @param type $type 验证码的类型
 24      */
 25     public function __construct($width = 160, $height = 40, $length = 4, $type = 1)
 26     {
 27         $this->width  = !empty($width)  ? $width  : $this->width;
 28         $this->height = !empty($height) ? $height : $this->height;
 29         $this->length = !empty($length) ? $length : $this->length;
 30         $this->type   = !empty($type)   ? $type   : $this->type;
 31     }
 32
 33     /**
 34      * 设置属性值
 35      * @param type $name 属性名
 36      * @param type $value 属性值
 37      */
 38     public function __set($name, $value)
 39     {
 40         if (isset($name)) {
 41             $this->$name = $value;
 42         }
 43     }
 44
 45     /**
 46      * 获取属性值
 47      * @param type $name 属性名
 48      * @return type 返回属性值
 49      */
 50     public function __get($name) {
 51         return $this->$name;
 52     }
 53
 54     /**
 55      * 校验验证码
 56      * @param type $code 表单提供的验证码
 57      * @return boolean
 58      */
 59     public function check($code){
 60         if (!isset($_SESSION)) {session_start();}
 61         if ($this->encodeVerify(strtolower($code)) === $_SESSION['code']){
 62             return true;
 63         }else{
 64             return false;
 65         }
 66     }
 67
 68     //输出验证码
 69     public function verify()
 70     {
 71         $this->code = $this->createVerify();
 72         //创建背景
 73         $this->createBackground();
 74         //文字显示
 75         $this->writeString();
 76         //画干扰线
 77         $this->paitLine();
 78         //输入图像
 79         $this->printImg();
 80     }
 81
 82     /**
 83      * 创建背景图片
 84      */
 85     private function createBackground()
 86     {
 87         //从图片库创建一个图像, 判断是否随机
 88         if ($this->bgRand){
 89             $img = imagecreatefromjpeg('./verify/bgs/'.mt_rand(1,8).'.jpg');
 90         }else{
 91             $img = imagecreatefromjpeg('./verify/bgs/'.$this->type.'.jpg');
 92         }
 93         //创建一个图片
 94         $this->img = imagecreatetruecolor($this->width, $this->height);
 95         //把图片复制到创建的图像上
 96         imagecopyresampled($this->img, $img, 0, 0, 0, 0, $this->width, $this->height, imagesx($img), imagesy($img));
 97     }
 98
 99     /**
100      * 在图片上写字
101      */
102     private function writeString()
103     {
104         $color = imagecolorallocatealpha($this->img, mt_rand(0,128), mt_rand(0,128), mt_rand(0,128), 0);
105         $fontType = './verify/ttfs/'.mt_rand(1,6).'.ttf';
106         $fontSize = mt_rand(15, 20);
107         for ($i = 0; $i < $this->length; $i++) {
108             $x = 3+($this->width/$this->length)*$i;
109             $y = mt_rand(($this->height/3)*2, ($this->height/3)*2);
110             //把验证码写在图片上
111             imagettftext($this->img, $fontSize, 0, $x, $y, $color, $fontType, $this->code[$i]);
112         }
113     }
114
115     /**
116      * 画干扰线和字母
117      */
118     private function paitLine()
119     {
120         $px = $py = 0;
121         $codes = '2345678abcdefhijkmnpqrstuvwxyz';
122         for ($i = 0; $i < $this->width/4; $i++){
123             $num = mt_rand(0, strlen($codes)-1);
124             $color = imagecolorallocatealpha($this->img, 255, 255, 255, 80);
125             //画字母
126             imagechar($this->img, 8, mt_rand(3, $this->width), mt_rand(3, $this->height), $codes{$num}, $color);
127         }
128     }
129
130     /**
131      * 输入图像
132      */
133     private function printImg()
134     {
135         if(function_exists('imagegif')){
136             // 针对 GIF
137             header('Content-Type: image/gif');
138             imagegif($this->img);
139         }elseif(function_exists('imagejpeg')){
140             // 针对 JPEG
141             header('Content-Type: image/jpeg');
142             imagejpeg($this->img, NULL, 100);
143         }elseif(function_exists('imagepng')){
144             // 针对 PNG
145             header('Content-Type: image/png');
146             imagepng($this->img);
147         }elseif(function_exists('imagewbmp')){
148             // 针对 WBMP
149             header('Content-Type: image/vnd.wap.wbmp');
150             imagewbmp($this->img);
151         }
152     }
153
154     /**
155      * 生成验证码
156      * @return string 返回生成的验证码
157      */
158     private function createVerify()
159     {
160         $codeSet = '2345678abcdefhijkmnpqrstuvwxyz';
161         $codes = '';
162         for ($i = 0; $i < $this->length; $i++) {
163             $codes .= $codeSet[mt_rand(0, strlen($codeSet)-1)];
164         }
165         //把验证码保存到session中
166         if (!isset($_SESSION)) {session_start();}
167         $_SESSION['code'] = $this->encodeVerify(strtolower($codes));
168 //        $_SESSION['code'] = $codes;
169         return $codes;
170     }
171
172     /**
173      * 加密验证码
174      * @param type $string
175      * @return type
176      */
177     private function encodeVerify($string)
178     {
179         $key = substr(md5($this->seKey), 5, 8);
180         $str = substr(md5($string), 8, 10);
181         return md5($key . $str);
182     }
183
184     /**
185      * 销毁图像
186      */
187     function __destruct()
188     {
189         if (isset($this->img)){
190             imagedestroy($this->img);
191         }
192     }
193 }

以上两步即可生生你想要的验证。

另外说明,Verify.class.php中有一个验证验证码是否正确的方法,使用如下

将你从界面中获得的验证码传入code方法中即可

if ($code -> code(这是传入你页面中获取的验证码值)){#这是验证正确的操作
}else{#验证失败的操作
}

以上就是我创建整个验证码的心得,希望对点击进来看的人有帮助。

转载于:https://www.cnblogs.com/xfjpeter/p/5813943.html

John细说PHP的验证码相关推荐

  1. 验证码研究入门必读(验证码是什么,有什么用,分类,设计,破解,未来发展)

           和实验室师姐们共同完成了一篇关于验证码的英文综述,在写综述的过程中看了很多验证码方面的论文,在本博客中我将以偏科普的方式介绍一下验证码,希望能够使对该领域有兴趣的同学对于验证码有进一步的 ...

  2. .NET下的验证码控件John.Controls.ValidateCode2V for .NET beta1

    预告下一个作品是选项卡John.Controls.TabularMultiView for .NET(难产ing,由于最近要处理的事情很多,身体上也有点不支,发布时间推迟) 注册了帐号好几久,都没发过 ...

  3. 【转】细说验证码安全 —— 测试思路大梳理

    细说验证码安全 -- 测试思路大梳理 1 前言 在安全领域,验证码主要分为两大类:操作验证码 和 身份验证码 虽然都是验证码,但是这两者所承担的职责却完全不同. 操作验证码,比如登录验证码,主要用来 ...

  4. PHP 验证码   高洛峰 细说PHP

    前端页面index.php <?php header('content-type:text/html;charset=utf-8'); if(isset($_POST['dosubmit'])) ...

  5. php高洛峰_PHP 验证码   高洛峰 细说PHP

    前端页面index.php<?php header('content-type:text/html;charset=utf-8'); if(isset($_POST['dosubmit'])){ ...

  6. PYDay6- 内置函数、验证码、文件操作、发送邮件函数

    1.内置函数 1.1Python的内置函数 abs() dict() help() min() setattr() all() dir() hex() next() slice() any() div ...

  7. 使用Java制作验证码

    验证码介绍 验证码(CAPTCHA)是"Completely Automated Public Turing test to tell Computers and Humans Apart& ...

  8. intellij idea写Springboot生成图片验证码两种实现方式(全码)

    最近工作中,需求让新加一个图片验证码功能,其实这个功能之前自己写过,想必跟大家现在心里想到的实现方式一样,要么是通过servlet实现请求操作,要么是通过get请求实现操作.然后在后台通过sessio ...

  9. CAPTCHA(验证码)的来源与作用

    国内好多人将验证码翻译为Validation Code,但是只要我们Google下,就会发现这是个Chinglish翻译.     验证码的来源           验证码的英文CAPTCHA 这个词 ...

最新文章

  1. 快捷键 = 效率,但 IDEA 快捷键记不住怎么办?
  2. python 画pr曲线
  3. 如何把一个二维数组的地址赋给一个二维指针?
  4. 2021-01-22 Python TimedRotatingFileHandler 修改suffix后无法自动删除文件
  5. 项目上传github步骤
  6. C++类对象在内存中的布局
  7. [SDOI2010]外星千足虫 题解 高斯消元+bitset简介
  8. Linux bash总结(一) 基础部分(适合初学者学习和非初学者参考)
  9. idea中event log_【JavaScript 教程】事件——Event 对象
  10. redux中间件原理-讲义
  11. [包计划] create-react-app
  12. win7 桌面背景保存位置,告诉你源文件删除后如何找回
  13. Odin WindowEditor使用体会
  14. 低通滤波器计算截止评率_了解奈奎斯特图中的截止频率
  15. 2019未来科学大奖周盛大开幕 百格活动倾情助力
  16. 李沐论文精读系列五:DALL·E2(生成模型串讲,从GANs、VE/VAE/VQ-VAE/DALL·E到扩散模型DDPM/ADM)
  17. 学习笔记-应用光学 第一章 几何光学的基本定律
  18. git fetch总结
  19. 什么是MES系统?MES系统具备哪些优势?
  20. 测试用例-——教室和椅子

热门文章

  1. 前端一HTML:二十二元素显示方式案例
  2. java batch批量
  3. python三十六:shelve模块
  4. Informatica PowerCenter使用介绍-转载
  5. js对html进行转义和反转义的操作
  6. 开发者需要了解的WebKit
  7. WindowsTime服务设置
  8. iis 服务器出现server too busy!
  9. MATLAB reshape()函数和sub2ind()函数
  10. android studio 7200u,超惊艳的设计!微软正式将Surface Studio和Surface Laptop带进中国:设计师们都看哭了...