php click captcha 验证码类

2023-05-16

需求:

现在常用的表单验证码大部分都是要用户输入为主,但这样对手机用户会不方便。

如果手机用户访问,可以不用输入,而是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" οnsubmit="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() ?>" style="cursor:pointer"></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 } ?>

源码下载:点击下载

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

php click captcha 验证码类 的相关文章

  • 使用 PHP 中的 GD 库在图像上绘图

    我创建了一个代码来生成随机图案图像 它创建一个具有给定宽度和高度的图像 并用 40x40 像素的小矩形填充它 这是我的代码
  • jQuery:单击外部元素以“关闭”使用toggleClass 出现的菜单

    我已经构建了一些导航 针对移动网络 它使用 jQuery 中的toggleClass 方法来隐藏和显示菜单 单击 MENU 图标 按钮可在菜单 div 上打开和关闭类 active 显示 隐藏 我一直在拼命寻找一种通过单击菜单外部 页面上的
  • if/else 简写来定义变量

    我很难理解 if else 的 php 简写是如何描述的here https stackoverflow com questions 20233207 php if shorthand and echo in one line possib
  • 禁用 WooCommerce 手动/编辑订单的电子邮件通知

    需要 WooCommerce 专业知识 我需要禁用手动创建的订单的电子邮件通知 我必须使用处理状态 由于处理订单状态的自定义挂钩 我无法创建自定义状态 理想情况下 手动订单页面中可以勾选一个复选框 勾选后 它将禁止在每种状态下向客户发送电子
  • PHP条件,如果当前页面,则链接突出显示[关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 我有一个带
  • 正则表达式上的换行符

    我试图替换两个标签之间的所有内容 但我无法构建正确的表达式 这就是我所做的
  • 在 PHP 中包含 PHP 文件

    我有一个网站 它运行 PHP if 语句来根据附加文件的类型 即 Jpg Txt MP4 显示内容 所以我显示 TXT 文件的代码是 if post attachment txt display attachment div class d
  • 尝试使用 php 发送 POST 请求,无论我做什么,我都会收到“HTTP ERROR 500”

    为了发出 HTTP 请求 有人建议我尝试使用 PHP 并给了我一段代码 url https example com dashboard api data array to gt PHONE NUMBER from gt SENDER ID
  • get url 重定向时 File_get_contents() 不起作用

    我正在使用的功能是 function http post url data data url http build query data data len strlen data url date default timezone set
  • PHP 无法打开流:是一个目录

    非常简单的 PHP 脚本 我在我亲自设置的 Ubuntu Web 服务器上的 EE 模板中运行 我知道这与权限有关 并且我已经将我尝试写入的目录的所有者更改为 Apache 用户 我得到的错误是 遇到 PHP 错误 严重性 警告 消息 fi
  • PHP 无法加载动态库“php_pdo_oci.dll”

    我在 Windows 8 上运行 Apache 2 4 7 和 PHP 5 5 9 我安装了 PHPUnit 并开始弹出此警告图像 警告 是的 我在 php ini 中启用了扩展加载以及 extension dir 以更正文件夹 并且该文件
  • Google Cloud SQL 上的故障转移如何运作?

    我打算将 PHP 应用程序 从 Google Cloud Platform 外部的服务器 连接到 Google Cloud SQL 我想知道如何设计应用程序以正确地对其数据库进行故障转移 根据manual https cloud googl
  • 通过 URL 指定控制器类与为每个控制器编写一个脚本相比,有何优缺点?

    今年夏天我安装了两个不同的 PHP 系统 每个都使用两种不同的方法 方法 1 每个任务一个 PHP 文件 该方法需要一个PHP为每个主要任务创建文件 例如 我的上传脚本可以通过http www domain com upload php O
  • WordPress 包含 SVG 文件错误

    我使用 PHP 和 WordPress 在本地主机上 我可以毫无问题地包含 SVG 文件 但在实时服务器上 我尝试包含一个 SVG 文件以便能够使用 CSS 对其进行样式设置 我收到此错误消息 Parse error syntax erro
  • 重写 URL,将 ID 替换为查询字符串中的标题

    我对 mod rewrite 很陌生 但我做了一些搜索 但找不到这个问题的答案 我有一个网站 它只有一个 PHP 页面 根据查询字符串中传递给它的 ID 提供数十页内容 我想重写 URL 以便此 ID消失并替换为从数据库中提取的页面标题 例
  • 如何通过ssh检查ubuntu服务器上是否存在php和apache

    如何通过ssh检查Ubuntu服务器上apache是 否安装了php和mysql 另外如果安装的话在哪个目录 如果安装了其他软件包 例如 lighttpd 那么它在哪里 确定程序是否已安装的另一种方法是使用which命令 它将显示您正在搜索
  • 从字符串中获取数字

    我有一个字符串 例如 lorem 110 ipusm 我想获取 110 我已经尝试过这个 preg match all 0 9 string ret 但这正在返回 Array 0 gt 1 1 gt 1 2 gt 0 我想要这样的东西 Ar
  • ZF3/2 - 如何捕获 EVENT_DISPATCH 侦听器中引发的异常?

    有什么方法可以在 EVENT DISPATCH 监听器中抛出异常吗 class Module public function onBootstrap EventInterface event application event gt get
  • 使用 crypt() 加密

    我目前正在做一个非常安全的登录系统 但我是 crypt 函数的新手 需要一些快速帮助 我在注册过程中使用 crypt 加密密码字符串并将其保存到数据库中 但是 我如何在登录过程中解密密钥 或者我应该怎么做 或者是否可以对提交的密码字符串进行
  • 如果产品重量超过1000克,如何以公斤为单位显示

    在 Storefront 主题中 我使用下面的代码将格式化重量从 1000g 更改为 1kg add action woocommerce after shop loop item title show weight 10 function

随机推荐

  • php CSS Update Class

    CSSUpdate class php lt php css 更新类 更新css文件内图片的版本 Date 2013 02 05 Author fdipzone Ver 1 1 Func update Ver 1 1 增加search ch
  • sh cssupdate

    shell sh 更新 css图片版本 bin bash csstmpl path 61 34 home fdipzone php csstmpl 34 css path 61 34 home fdipzone php css 34 rep
  • JS小游戏-宇宙战机

    游戏介绍 业余时间写的一个飞行射击游戏 xff0c 纵向 xff0c 共六关 游戏需求 1 战机可发射子弹 xff0c 子弹可通过获取道具升级 2 战机可放bomb xff0c 可获取道具增加数量 3 战机可蓄力攻击 4 道具有三种 xff
  • php __call 与 __callStatic

    php 5 3 后新增了 call 与 callStatic 魔法方法 call 当要调用的方法不存在或权限不足时 xff0c 会自动调用 call 方法 callStatic 当调用的静态方法不存在或权限不足时 xff0c 会自动调用 c
  • $CF1153A\ Serval\ and\ Bus$

    看大佬的代码都好复杂 xff08 不愧是大佬 orz 蒟蒻提供一种思路 因为求的是最近的车对吧 qwq 所以我们可以用一个 while 循环所以没必要去用什么 for 至于这是 div2 的第一题还是比较水的 code include lt
  • Sublime Text配置JDK

    操作系统 xff1a Windows 7 SP1 Sublime Text是一款轻量级代码编辑器 虽然收费 xff0c 但可以无限期试用 支持多种语言的代码高亮 xff0c 但一些不能直接编译运行 xff0c 今天我为大家带来Sublime
  • JS小游戏-仙剑翻牌

    游戏介绍 这是一个翻牌配对游戏 xff0c 共十关 1 游戏随机从42张牌中抽取9张进行游戏 xff0c 每组为2张相同的牌 xff0c 共18张牌 2 连续翻到两张相同的为胜利 xff0c 当9组全部翻到则过关 如不是翻到连续两张相同的
  • memcached 常用命令及使用说明

    memcached 查看方法 格式 telnet ip port 例如 telnet localhost 11211 退出命令 xff1a quit 一 存储命令 存储命令格式 xff1a lt command name gt lt key
  • PHPMailer - PHP email transport class

    在服务器安装 sendmail sudo apt get install sendmail 启动 sendmail sudo etc init d sendmail start 修改 php ini mail function SMTP 6
  • PHP 遍历文件夹及文件类及处理类

    FindFile class php 用于遍历目录文件 lt php 遍历文件夹及文件类 Date 2013 03 21 Author fdipzone Ver 1 0 class FindFile public files 61 arra
  • sh autolog backup

    shell sh 每天备份log文件 bin bash 每天备份log文件 log path 61 34 home fdipzone logs 34 log目录 backup path 61 34 home fdipzone logs ba
  • Apache rewrite

    1 开启rewrite sudo a2enmod rewrite 2 停用rewrite sudo a2dismod rewrite 3 服务器环境变量 Apache提供给rewirte模块的环境变量大概分成5个类型 第一部分 HTTP h
  • RewriteCond和13个mod_rewrite应用举例Apache伪静态

    1 xff0e 给子域名加www标记 RewriteCond HTTP HOST a z 43 example com NC RewriteCond HTTP HOST www NC RewriteRule http www xample
  • 正向代理与反向代理的区别

    正向代理的概念 正向代理 也就是传说中的代理 他的工作原理就像一个跳板 简单的说 我是一个用户 我访问不了某网站 但是我能访问一个代理服务器 这个代理服务器呢 他能访问那个我不能访问的网站 于是我先连上代理服务器 告诉他我需要那个无法访问网
  • Apache 搭建虚拟主机

    Apache 搭建虚拟主机方法 DocumentRoot xff1a home fdipzone sites demo fdipzone com ServerName xff1a demo fdipzone com 1 进入apache虚拟
  • sh memcached 进程启动及监控

    memcached 进程启动及监控 1 memcached inc sh 设置路径 xff0c 端口等讯息 bin sh config include HOST 61 hostname SITE 61 34 mysite 34 PORT 6
  • 设置进程的显示名称

    有时候在LINUX下 xff0c fork子进程的时候 xff0c 像nginx里的一样 xff0c 想让子进程的名字可以自定义 参考网上文章之后 xff0c 可以通过修改argv 0 的值来改变子进程的名字 xff0c 但是要注意新标题的
  • 自动登入google play下载app report

    流程 1 登入google play 登入google play需要三步 https play google com apps publish https accounts google com ServiceLogin hl 61 en
  • sh cssupdate 优化

    bin bash 更新css文件内图片的版本 如background url 39 images test jpg 39 更新为 background url 39 images test jpg 20130330121210 39 css
  • php click captcha 验证码类

    需求 xff1a 现在常用的表单验证码大部分都是要用户输入为主 xff0c 但这样对手机用户会不方便 如果手机用户访问 xff0c 可以不用输入 xff0c 而是click某一位置便可确认验证码 xff0c 这样就会方便很多 原理 xff1