php实现的click captcha点击验证码类实例,clickcaptcha
本文实例讲述了php实现的click captcha点击验证码类及其用法,是非常实用的功能。分享给大家供大家参考之用。具体如下:
一、需求:
现在常用的表单验证码大部分都是要用户输入为主,但这样对手机用户会不方便。
如果手机用户访问,可以不用输入,而是click某一位置便可确认验证码,这样就会方便很多。
二、原理:
1.使用PHP imagecreate创建PNG图象,在图中画N个圆弧,其中一个是完整的圆(验证用),将圆心坐标及半径记录入session。
2.在浏览器,当用户在验证码图片上点击时,记录点击的位置。
3.将用户点击的坐标与session记录的圆心坐标、半径比较,判断是否在圆中,如是则验证通过。
程序运行效果如下图所示:
三、实现方法:
ClickCaptcha.class.php类文件如下:
<?php /** Click Captcha 验证码类 * Date: 2013-05-04 * Author: fdipzone * Ver: 1.0 */ class ClickCaptcha { // class start public $sess_name = 'm_captcha'; public $width = 500; public $height = 200; public $icon = 5; public $iconColor = array(255, 255, 0); public $backgroundColor = array(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; // 设置session name } } /** 创建验证码 */ public function create(){ // 创建图象 $this->_img_res = imagecreate($this->width, $this->height); // 填充背景 ImageColorAllocate($this->_img_res, $this->backgroundColor[0], $this->backgroundColor[1], $this->backgroundColor[2]); // 分配颜色 $col_ellipse = imagecolorallocate($this->_img_res, $this->iconColor[0], $this->iconColor[1], $this->iconColor[2]); $minArea = $this->iconSize/2+3; // 混淆用图象,不完整的圆 for($i=0; $i<$this->icon; $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)); // 生成图象 Header("Content-type: image/PNG"); ImagePNG($this->_img_res); ImageDestroy($this->_img_res); exit(); } /** 检查验证码 * @param String $captcha 验证码 * @param int $flag 验证成功后 0:不清除session 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,''); } return true; } } return 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 */ private function pointInArea($px, $py, $cx, $cy, $cr){ $x = $cx-$px; $y = $cy-$py; return round(sqrt($x*$x + $y*$y))<$cr; } /** 验证码session处理方法 * @param String $name captcha session name * @param String $value * @return String */ private function captcha_session($name,$value=null){ if(isset($value)){ if($value!==''){ $_SESSION[$name] = $value; }else{ unset($_SESSION[$name]); } }else{ return isset($_SESSION[$name])? $_SESSION[$name] : ''; } } } // class end ?>
demo.php示例程序如下:
<?php session_start(); require('ClickCaptcha.class.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']) : ''; $captcha = isset($_POST['captcha'])? trim($_POST['captcha']) : ''; $obj = new ClickCaptcha(); if($obj->check($captcha)){ echo 'your name is:'.$name; }else{ echo 'captcha not match'; } echo ' <a href="demo.php">back</a>'; }else{ // html ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title> Click Captcha Demo </title> <script type="text/javascript" src="jquery-1.6.2.min.js"></script> <script type="text/javascript"> $(function(){ $('#captcha_img').click(function(e){ var x = e.pageX - $(this).offset().left; var y = e.pageY - $(this).offset().top; $('#captcha').val(x+','+y); }) $('#btn').click(function(e){ if($.trim($('#name').val())==''){ alert('Please input name!'); return false; } if($.trim($('#captcha').val())==''){ alert('Please click captcha!'); return false; } $('#form1')[0].submit(); }) }) </script> </head> <body> <form name="form1" id="form1" method="post" action="demo.php" onsubmit="return false"> <p>name:<input type="text" name="name" id="name"></p> <p>Captcha:Please click full circle<br><img id="captcha_img" src="demo.php?get_captcha=1&t=<?=time() ? alt="php实现的click captcha点击验证码类实例,clickcaptcha_PHP教程" >" style="max-width:90%"></p> <p><input type="submit" id="btn" value="submit"></p> <input type="hidden" name="send" value="true"> <input type="hidden" name="captcha" id="captcha"> </form> </body> </html> <?php } ?>
本文完整源码点击此处本站下载。
希望本文所述对大家的PHP程序设计有所帮助。
你要判断啊,判断你这个填写的验证码是不是跟你之前验证码保存的是否一致
if($_POST["captcha"]!=$_SESSION["captcha"])
{
echo "验证码错误";
}
如果你的意思是 Ajax 那样的效果的话,那么可以用类似这样的方式 :$captcha = new Zend_Captcha_Image(array(......));$id = $captcha->generate();echo \'\' . $captcha->render($view) . \'\';// .jsfunction changeCaptcha(){? ???......? ???document.getElementById(\'captcha\').innerHtml = ......}

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。

在PHP中,trait适用于需要方法复用但不适合使用继承的情况。1)trait允许在类中复用方法,避免多重继承复杂性。2)使用trait时需注意方法冲突,可通过insteadof和as关键字解决。3)应避免过度使用trait,保持其单一职责,以优化性能和提高代码可维护性。

依赖注入容器(DIC)是一种管理和提供对象依赖关系的工具,用于PHP项目中。DIC的主要好处包括:1.解耦,使组件独立,代码易维护和测试;2.灵活性,易替换或修改依赖关系;3.可测试性,方便注入mock对象进行单元测试。

SplFixedArray在PHP中是一种固定大小的数组,适用于需要高性能和低内存使用量的场景。1)它在创建时需指定大小,避免动态调整带来的开销。2)基于C语言数组,直接操作内存,访问速度快。3)适合大规模数据处理和内存敏感环境,但需谨慎使用,因其大小固定。

PHP通过$\_FILES变量处理文件上传,确保安全性的方法包括:1.检查上传错误,2.验证文件类型和大小,3.防止文件覆盖,4.移动文件到永久存储位置。

JavaScript中处理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。1.??返回第一个非null或非undefined的操作数。2.??=将变量赋值为右操作数的值,但前提是该变量为null或undefined。这些操作符简化了代码逻辑,提高了可读性和性能。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 英文版
推荐:为Win版本,支持代码提示!