宝哥软件园

点击验证码点击验证码类示例用php实现

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

本文介绍了用php实现的click captcha ClickCaptcha类及其用法,这是一个非常实用的功能。分享给大家参考。具体如下:

一.需求:

现在常用的表单验证码大多需要用户输入,但这对于手机用户来说并不方便。如果手机用户来访,可以通过点击某个位置确认验证码,而不用输入,方便多了。

二、原则:

1.使用PHP imagecreate创建PNG图像,在图像中绘制n个圆弧,其中一个为完整的圆(用于验证),并将中心坐标和半径记录到session中。

2.在浏览器中,当用户点击验证码图片时,记录下被点击的位置。

3.将用户点击的坐标与会话记录的中心坐标和半径进行比较,判断是否在圆内。如果是,验证通过。

程序的运行效果如下图所示:

三、实施方法:

ClickCaptcha.class.php的文件如下:

?php /**点击验证码验证码类*日期: 2013-05-04 *作者: fdipzone * ver : 1.0 */class点击验证码{//class start public $ sess _ name=' m _ captcha ';public $ width=500公共$ height=200 public $ icon=5;public $iconColor=array(255,255,0);public $backgroundColor=数组(0,0,0);public $ IConSize=56 private $ _ img _ RES=null public function _ _ construct($ sess _ name=' '){ if(session _ id()==' '){ session _ start();} if($sess_name!=' '){ $ this-sess _ name=$ sess _ name;//设置会话名称} } /**创建验证码*/public函数create(){ //创建图象$ this-_ img _ RES=image create($ this-width,$ this-height);//填充背景image color allocate($ this-_ img _ RES,$this-backgroundColor[0],$this-backgroundColor[1],$ this-background color[2]);//分配颜色$ col _ ellipse=image color allocate($ this-_ img _ RES,$this-iconColor[0],$this-iconColor[1],$ this-icon color[2]);$ minarea=$ this-icon size/2 ^ 3;//混淆用图象,不完整的圆对于($ I=0;一美元这个图标;$i ){ $x=mt_rand($minArea,$ this-width-$ MInarea);$y=mt_rand($minArea,$ this-height-$ MInarea);$s=mt_rand(0,360);$ e=$ s 330 imagearc($ this-_ img _ RES,$x,$y,$this-iconSize,$this-iconSize,$ s,$ e,$ col _ ellipse);} //验证用图象,完整的圆$x=mt_rand($minArea,$ this-width-$ MInarea);$y=mt_rand($minArea,$ this-height-$ MInarea);$ r=$ this-IConSize/2;imagearc($this-_img_res,$x,$y,$this-iconSize,$this-iconSize,0,360,$ col _ ellipse);//记录圆心坐标及半径$ this-captcha _ session($ this-sess _ name,array($x,$y,$ r));//生成图象标题(“内容类型:图像/巴布亚新几内亚”);ImagePNG($ this-_ img _ RES);图像销毁($ this-_ img _ RES);exit();} /** 检查验证码* @ param String $验证码验证码* @ param int $标志验证成功后0:不清除会话1:清除session * @ return boolean */public function check($ captcha,$ flag=1){ if(trim($ captcha)='){ return false;} if(!is _ array($ this-captcha _ session($ this-sess _ name)){ return false;} list($px,$py)=explode(',',$ captcha);list($cx,$cy,$ Cr)=$ this-captcha _ session($ this-sess _ name);if(isset($ px)is _ numeric($ px)isset($py)is _ numeric($ py)isset($cx)is _ numeric($ CX)isset($cy)is _ numeric($ cy)isset($ Cr)is _ numeric($ Cr)){ if($ this-pointInArea($ px,$ py,$ CX,$ cy,$ Cr)){ if($ flag==1){ $ this-captcha _ session($ this-sess _ name ' ';}返回真实} }返回false} /**判断点是否在圆中* @param int $px点x * @param int $py点y * @param int $cx圆心x * @param int $cy圆心y * @param int $cr圆半径* sqrt(x^2 y^2)r *私有函数pointInArea($px,$py,$cx,$cy,$ Cr){ $ x=$ CX-$ px;$ y=$ cy-$ py;返回回合(sqrt($ x * $ x $ y * $ y))$ Cr;} /** 验证码会议处理方法* @param String $name验证码会话名称* @ param String $ value * @返回String */private函数验证码_session($name,$ value=null){ if(isset($ value)){ if($ value!==' '){ $ _ SESSION[$ name]=$ value;} else { unset($ _ SESSION[$ name]);} } else { return isset($ _ SESSION[$ name])?$ _ SESSION[$ name]: " ";} } } //类结束?demo.php示例程序如下:

?PHP session _ start();需要('点击验证码。上课。PHP’);if(isset($ _ GET[' GET _ captcha '])){//GET captcha $ obj=new ClickCaptcha();$ obj-create();exit();} if(isset($ _ POST[' send '])$ _ POST[' send ']==' true '){//submit $ name=isset($ _ POST[' name '])?trim($ _ POST[' name ']): " ";$验证码=isset($_POST['验证码'])?trim($ _ POST[' captcha ']): " ";$ obj=new ClickCaptcha();if($obj-check($captcha)){ echo '你的名字是: '。$ name}else{ echo '验证码不匹配;} echo"a href="demo。PHP“后退/a”;}else{ //html?DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN ' ' http://www .w3。org/TR/HTML 4/松散。DTD ' HTML head meta http-equiv=' content-type ' content=' text/HTML;字符集=utf-8 '标题单击验证码演示/标题脚本类型=' text/JAVAScript ' src=' http : jquery-1。6 .2 .量滴js /脚本脚本类型=' text/JavaScript ' $(function(){ $(' # Captcha _ img ')).点击(函数(e){ var x=e.pageX - $(this).偏移量()。向左;var y=e.pageY - $(this).偏移量()。顶部;$(' #验证码').val(x ',' y);}) $('#btn ').点击(函数{如果($).修剪($(“# name”).val())==''){ alert('请输入名称!');返回false} if($ .修剪($(' #验证码').val())==''){ alert('请点击验证码!');返回false } $(“# form 1”)[0].submit();})})))/脚本/头体表单名称=' form 1 ' id=' form 1 '方法=' post '操作=' demo。PHP ' on submit=' return false ' pname :输入类型=' text ' name=' name ' id=' name '/p pcaptcha :请单击全圆brimg id=' captcha _ img ' src=' http : demo。PHP?get_captcha=1t=?=time()?style=' cursor :指针'/p pinput type=' submit ' id=' BTN ' value=' submit '/p输入类型=' hidden ' name=' send ' value=' true '输入类型=' hidden ' name='验证码id='验证码/表单/正文/html?php }?本文完整源码点击此处本站下载。

希望本文所述对大家的服务器端编程语言(专业超文本预处理器的缩写)程序设计有所帮助。

更多资讯
游戏推荐
更多+