感谢大家的收藏和好评,这是对我最大的鼓励。本系列文章主要是一些常用的PHP类操作。回过头来看面向对象,我一直在做小白文章的写作,因为我也是小白。相信有人需要,也希望读者喜欢。说明关于PHP语言的各种操作的实现,大家可以在网上找到,但是质量参差不齐,有些代码不太好理解,有时候看得眼花缭乱,所以在这里统一整理一下(注:是安排,每一种操作,我都会参考至少三个优秀的代码,然后自己跑出来,而不是CV)我会尽量遵守PSR规范,会有很详细易懂的-理解评论涉及到的相关知识点,比如设计模式、类型检测、浏览器架构、通信数据,我也会简单说一下,并留下相关链接,希望读者慢慢咀嚼,仔细阅读。准备内容涉及面向对象、数据库、文件操作、购物车、分页、图片处理、JSON数据接口,可能需要你有一定的知识基础,当然这些内容是独立的,你可以有选择地参考。为什么需要验证码?虽然现在很多商家直接通过短信验证用户身份,但仍有很多场景和使用习惯需要使用验证码;另外,在登录网站时,为了提高网站的安全性,屏蔽机器请求,避免用户“浇水”等行为,往往需要输入各种验证码。验证码是什么?Captcha是TuringTesttoAutomaticallyDistinguishComputersandHumans的首字母缩写,它是一个完全自动化的程序,可以区分计算机和人类用户。验证码的类型?文字、数字、字母(统称字符)、图片、语音手势、验证码的设计思路是一致的。让我们以字符为例。验证设计1.服务端生成验证码并异步传输到HTML页面2.服务端将生成的验证码放入session3.HTML表单提交4.后台匹配(比较表单值和session值)如图:源代码width=$w;$this->height=$h;$this->numbers=$n;$this->imageType=$imageType;$this->codeType=$codeType;/*生成随机验证字符串*/$this->codeString=$this->createCode($this->codeType);$this->show();}privatefunctioncreateCode($type){switch($type){case1:/*生成纯数字,先使用range(0,9)生成数组*通过$this->verifyNums确定字符串个数*使用array_rand()从数组中随机获取对应的数字*使用join将数字拼接成字符串存入$this->codeString*/$this->codeString=join('',array_rand(range(0,9),$this->数字));休息;case2:/*生成纯字母,*小写字母数组range('a','z')*大写字母数组range('A','Z')*合并两个数组array_merge()*替换key和valuearray_filp()*随机获取array特定数量的元素array_rand()*拼接成字符串implode()*/$this->codeString=implode(array_rand(array_filp(array_merge(range('a','z'),range('A','Z'))),$this->numbers));case3://混合类型$words=str_shuffle('abcdefghjkmpopqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY3456789');$this->codeString=substr($words,0,$this->numbers);休息;}返回$this->codeString;}//开始准备生成图片/*方法名:show()*功能:调用生成图片的所有方法*/privatefunctionshow(){$this->createImg();//创建图片资源$this->填充背景();//填充背景色$this->fillPix();//填充干扰点$this->fillArc();//填充干扰弧$this->writeFont();//写入$this->outPutImg();//输出图片}//创建图片资源:imagecreatetruecolor($width,$height)privatefunctioncreateImg(){$this->resource=imagecreatetruecolor($this->width,$this->height);}//填充背景色:imagefill($res,$x,$y,$color)//随机生成暗色--->imagecolorallocate($res,$r,$g,$b)privatefunctionsetDarkColor(){returnimagecolorallocate($this->resource,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));}//随机生成光色privatefunctionsetLightColor(){returnimagecolorallocate($this->resource,mt_rand(0,130),mt_rand(0,130),mt_rand(0,130));}//开始填充privatefunctionfillBackground(){imagefill($this->resource,0,0,$this->setDarkColor());}//随机生成干扰点-->imagesetpixelprivatefunctionfillPix(){//计算生成多少个干扰点(单个像素),这里设置每20个像素生成一个$num=ceil(($this->width*$this->height)/20);for($i=0;$i<$num;$i++){图像集像素($this->resource,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->设置暗色());}}//随机绘制10个圆弧->imagearc()privatefunctionfillArc(){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());}}/*在canvas上写文字*根据字符数,将canvas横向分成相应的块$every=ceil($this->width/$this->verifyNums);*在每个小块的随机位置绘制相应的字符imagechar();*/privatefunctionwriteFont(){$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());}}//输出图片资源privatefunctionoutPutImg(){//header('Content-type:image/imagetype')header('Content-type:image/'.$this->imageType);//根据图片类型调用不同的方法输出图片//imagepng($img)/imagejpg($img)$func='image'.$this->imageType;$func($this->资源);}//设置验证码字符只能调用,不能修改,验证验证码是否输入正确公共函数__get($name){if($name='codeString'){return$this->codeString;}}//销毁方法,自动销毁图片资源publicfunction__destruct(){imagedestroy($this->resource);}}//新代码;
