php验证码类库,PHP常用操作类实现——验证码类

概述

谢谢大家的收藏与赞,这是对我最大的鼓励。

这一系列文章主要是一些PHP常用的类操作,回顾一下面向对象,我一直致力于小白文章的撰写,因为我也是小白,相信有人需要,希望读者能够喜欢。

说明

关于PHP语言实现各种操作类,网上都能够找得到,但是质量参差不齐,有的代码不好懂,有时候看的眼花缭乱,所以我在这里统一作了一个整理(注意:是整理,每一种操作类,我至少会参考三份优秀代码,然后自己跑出来,而不是CV)

我会尽量遵守PSR规范,会有很详细易懂的注释

对于其中涉及到的相关知识点,比如设计模式,类型检测、浏览器架构、通信数据我也会简单讲讲,并留下相关链接,希望读者能够细嚼慢咽

阅读准备

内容涉及面向对象、数据库、文件操作、购物车、分页、图像处理、JSON数据接口,你可能需要有一点知识基础,当然,这些内容都是独立的,可以选择性的参看。

为什么要验证码?

尽管现在很多业务是直接通过短信验证用户身份,但是还是有许多场景和使用习惯需要用到验证码;另外, 在登录网站时,为了提高网站的安全性,屏蔽机器请求,避免用户”灌水“等行为,经常需要输入各种各样的验证码。

验证码是什么?

验证码是自动区分计算机和人类的图灵测试的缩写,是一种区分用户是计算机和人的全自动程序。

验证码的种类?

文字、数字、字母(统称字符)

图片

语音

手势

对于不同的验证码,验证的设计思路是一致的,我们以字符为例。

验证设计

1.服务器生成验证码异步传输到HTML页面

2.服务器将生成的验证码放入session

3.HTML表单提交

4.后台匹配(将表单值与session值对比)

如图:

b0081b6759615f84fb0560a28c5eb6bb.png

Source Code

/**

* TODO:PHP-验证码类

* Author:entner (wangyiicloud@icloud.com)

* time: 2017-10-17

* version:1.0

* ready:

{

属性:

宽度 int $width

高度 int $hight

干扰点

验证码(私有的)

验证码字符的个数 int $number

验证码的类型(纯数字类型、纯字母类型、混合类型)

背景颜色 string $color

字体颜色 string $fontColor

图片类型(jpg、png、bmp……) string $imageType

图片资源 string $resource

功能:

构造函数(用来初始化) function

生成验证码的函数 function createCode

创建画布 function createImage

填充背景色 function fillBackground

填充干扰点

填充干扰弧线

画验证码

输出图片

}

*/

Class Code{

public $width;

public $height;

public $numbers;

public $codeType; //1.数字 2.字母 3.混合

public $color;

public $fontColor;

public $imageType;

private $codeString;//验证字符串母体

private $resource; //图片资源

public function __construct($w=100,$h=50,$n=4,$imageType='png',$codeType=1){

$this->width = $w;

$this->height = $h;

$this->numbers = $n;

$this->imageType = $imageType;

$this->codeType = $codeType;

/* 生成随机的验证字符串 */

$this->codeString = $this->createCode($this->codeType);

$this->show();

}

private function createCode($type){

switch($type){

case 1:

/*生成纯数字,首先使用range(0,9)生成数组

*通过$this->verifyNums确定字符串的个数

*使用array_rand()从数组中随机获取相应个数

*使用join将数字拼接成字符串,存储到$this->codeString中

*/

$this->codeString = join('',array_rand(range(0, 9),$this->numbers));

break;

case 2:

/*生成纯字母,

*小写字母数组range('a','z')

*大写字母数组range('A','Z')

*合并两个数组array_merge()

*更换键和值 array_filp()

*随机获取数组中的特定个数的元素 array_rand()

*拼接成字符串 implode()

*/

$this->codeString = implode(array_rand(array_filp(array_merge(range('a','z'),range('A','Z'))),$this->numbers));

case 3:

//混合类型

$words = str_shuffle('abcdefghjkmpopqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY3456789');

$this->codeString = substr($words,0,$this->numbers);

break;

}

return $this->codeString;

}

//开始准备生成图片

/*方法名:show()

*功能 :调用生成图片的所有方法

*/

private function show(){

$this->createImg();//创建图片资源

$this->fillBackground(); //填充背景颜色

$this->fillPix(); //填充干扰点

$this->fillArc(); //填充干扰弧线

$this->writeFont();//写字

$this->outPutImg();//输出图片

}

//创建图片资源:imagecreatetruecolor($width,$height)

private function createImg(){

$this->resource = imagecreatetruecolor($this->width,$this->height);

}

//填充背景颜色:imagefill($res,$x,$y,$color)

//随机生成深色--->imagecolorallocate($res,$r,$g,$b)

private function setDarkColor()

{

return imagecolorallocate($this->resource,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));

}

//随机生成浅色

private function setLightColor()

{

return imagecolorallocate($this->resource,mt_rand(0,130),mt_rand(0,130),mt_rand(0,130));

}

//开始填充

private function fillBackground()

{

imagefill($this->resource,0,0,$this->setDarkColor());

}

//随机生成干扰点-->imagesetpixel

private function fillPix()

{

//计算产生多少个干扰点(单一像素),这里设置每20个像素产生一个

$num = ceil(($this->width * $this->height) / 20);

for($i = 0; $i < $num; $i++){

imagesetpixel($this->resource,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->setDarkColor());

}

}

//随机画10条弧线->imagearc()

private function fillArc()

{

for($i = 0;$i < 10;$i++){

imagearc($this->resource,

mt_rand(10,$this->width-10),

mt_rand(5,$this->height-5),

mt_rand(0,$this->width),

mt_rand(0,$this->height),

mt_rand(0,180),

mt_rand(181,360),

$this->setDarkColor());

}

}

/*在画布上写文字

*根据字符的个数,将画布横向分成相应的块

$every = ceil($this->width/$this->verifyNums);

*每一个小块的随机位置画上对应的字符

imagechar();

*/

private function writeFont()

{

$every = ceil($this->width / $this->numbers);

for($i = 0;$i < $this->numbers;$i++){

$x = mt_rand(($every * $i) + 5,$every * ($i + 1) - 5);

$y = mt_rand(5,$this->height - 10);

imagechar($this->resource,6,$x,$y,$this->codeString[$i],$this->setLightColor());

}

}

//输出图片资源

private function outPutImg()

{

//header('Content-type:image/图片类型')

header('Content-type:image/'.$this->imageType);

//根据图片类型,调用不同的方法输出图片

//imagepng($img)/imagejpg($img)

$func = 'image'.$this->imageType;

$func($this->resource);

}

//设置验证码字符只能调用,不能修改,用来验证验证码是否输入正确

public function __get($name){

if($name = 'codeString'){

return $this->codeString;

}

}

//析构方法,自动销毁图片资源

public function __destruct()

{

imagedestroy($this->resource);

}

}

//new Code;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值