手机版

PHP使用session和gd库实现简单验证码生成和验证的类方法

时间:2021-09-12 来源:互联网 编辑:宝哥软件园 浏览:

验证码是为了防止机器灌水造成网站污染,增加服务器负担而设计的。目前大大小小的网站都有验证码。今天,我实现了一个简单的验证码类。很简单,因为它没有添加一些干扰弧等,只是旋转文本。当然,因为字体的原因,不容易一眼看出来。同时,为了避免字母大写和数字混淆,去掉了看起来非常相似的字母和数字。类别:

?Php/** *只需生成验证码类*/class Captcha { private $ width;//验证码的宽度private $ height//验证码高度私有$ countOfChars//字符数//private $ distributures;//干扰行号private $ chars//随机生成的字符串public function _ _ construct($ width=100,$ height=30,$ chars的计数=4,$ distribubliens=2){//初始化参数$ this-width=$ width;$ this-height=$ height;$ this-countOfChars=$ countOfChars;session _ start();}/* * * $ this-create chars();$ this-draw chars($ imageHandle);$ this-outImage($ imageHandle);}/* * *创建画布并用颜色随机填充* @return返回画布句柄*/public函数Create image(){ $ image handle=image Create($ this-width,$ this-height);//随机背景色$ randcolor=imagecolor locate($ imagehandle,50,mt _ rand (0,50),mt _ rand (0,50));imagefill($imageHandle,0,0,$ randColor);返回$ imageHandle}/* * *生成随机字符*/private函数create chars(){//候选字符$ str=' abcdefghjklmnpqrstuvwxzabbcdefghijkkmnpqtuvw x 2346789 ';$ chars=for($ I=0;$ I $ this-count of chars;$i ){ $chars。=$str[mt_rand(0,strlen($ str)-1)];} $ this-chars=$ chars;//save in session $ _ session[' captcha ']=strtolow($ chars);}/* * *将字符写入图像* @param type $imageHandle图像句柄*/private函数draw chars($ imageHandle){ if($ this-chars!=null){ $ font='/home/WWW/yuweilishuft . TTF ';for($ I=0;$ is tren($ this-chars);$ I){ $ color=imagecolor allocate($ imageHandle,mt_rand(50,200),mt_rand(100,255),255);imagefttext($imageHandle,30,30,$i*20 10,25,$color,$font,$ this-chars[$ I]);}}}/* * *输出图像* @param type $imageHandle图像句柄*/私有函数outimage($ imageHandle){ imagepng($ imageHandle);image destroy($ imageHandle);}/* * *判断用户输入的验证码是否正确* @param type $usrInput判断用户输入的验证码* @ return boolean matches */public static函数是否正确($ usrInput){ if(isset($ _ SESSION[' captcha ']){ if(strtolow($ usrInput)=$ _ SESSION[' captcha ']){ $ _ SESSION[' captcha ']=null;返回真;} else { $ _ SESSION[' captcha ']=null;返回false}}}}将验证设置为静态方法,因为验证码生成后已经保存在会话中,验证时直接调用静态方法,不实例化这个类。

以上字体可以随意设置。

下面的代码返回一个图像,它是在实例化验证码类后动态生成的。(outCaptcha.php)

?phprequire(' Captcha . PHP ');$ code=new Captcha();标题(' Content-type : IMage/png ');$ code-execute();标题(' Content-type : IMage/png ');

这句话的作用是告诉浏览器输出的是png图像,不是html代码。收到后,浏览器会将以下输出解析为图像。

然后写一个html静态页面(testCaptcha.html)并创建一个表单

!测试doctype html头标题验证码/title meta charset=' utf-8 ' meta name=' viewport ' content=' width=device-width,Initial-scale=1.0 '/头体h1,请输入验证码:/h1img src=' http : outcaptcha . PHP '/Form方法=' post' action=' prove.php '输入类型=' text ' name=' input _ captcha '/button name=' submit '确保/button /form /body/html看到提交表格的地址了吗?这是用于验证验证码是否输入正确的代码:

session _ start();$ input captcha=trim($ _ POST[' input _ captcha ']);需要(' Captcha . PHP ');如果(验证码:正确($输入验证码)){echo '验证码正确';}else{ echo '错误或过期的验证码';} session _ destroy();这里我们还是需要导入验证码类,然后调用它的静态方法来验证你的输入。最后,销毁整个会话。

最后,看效果

太好了,成功了。然后尽量故意出错,退后一步刷新(如果不刷新浏览器,会直接调用缓存中的验证码图片,我们的验证码还没有生成!所以无论如何都会出错)。

当然,真正的验证码可以通过点击来更改,这就利用了ajax技术。

以上用PHP的类方法实现简单的验证码生成和用session和gd库验证都是边肖分享的内容。希望能给大家一个参考,支持我们。

版权声明:PHP使用session和gd库实现简单验证码生成和验证的类方法是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。