php+js实现的拖动滑块验证码验证表单操作示例【附源码下载】


Posted in PHP onMay 27, 2020

本文实例讲述了php+js实现的拖动滑块验证码验证表单操作。分享给大家供大家参考,具体如下:

现在很多网站,比如淘宝,京东等都改用使用极验拖动验证码实现登录,这种方式比传统的验证码方式有更好的体验,减少用户输入的错误,也同样能起到防盗刷的功能。现在很多极验都是第三方的,也很多都是收费的。今天在这里给大家分享自己用原生php实现的一个极验的代码。用原生php的好处就是以后你要嵌套到什么框架,可以直接用核心代码,改一改就好了。

极验拖动动画图

php+js实现的拖动滑块验证码验证表单操作示例【附源码下载】

代码文件截图

php+js实现的拖动滑块验证码验证表单操作示例【附源码下载】

代码实现

html文件

<!DOCTYPE html>
<html lang="">
<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>极验滑块拖动验证码-码农社区-web视频分享网</title>
  <script type="text/javascript" src="tn_code.js?v=35"></script>
  <link rel="stylesheet" type="text/css" href="style.css?v=27" rel="external nofollow" />
<style type="text/css"></style>
</head>
<body style="text-align:center;">
<div class="tncode" style="text-align: center;margin: 100px auto;"></div>
<script type="text/javascript">
$TN.onsuccess(function(){
//验证通过
});
</script>

php文件:check.php

<?php
require_once dirname(__FILE__).'/TnCode.class.php';
$tn = new TnCode();
if($tn->check()){
    $_SESSION['tncode_check'] = 'ok';
  echo "ok";
}else{
    $_SESSION['tncode_check'] = 'error';
  echo "error";
}

?>

主要核心文件:TnCode.class.php

<?php
class TnCode
{
  var $im = null;
  var $im_fullbg = null;
  var $im_bg = null;
  var $im_slide = null;
  var $bg_width = 240;
  var $bg_height = 150;
  var $mark_width = 50;
  var $mark_height = 50;
  var $bg_num = 6;
  var $_x = 0;
  var $_y = 0;
  //容错象素 越大体验越好,越小破解难道越高
  var $_fault = 3;
  function __construct(){
    //ini_set('display_errors','On');
    //
    error_reporting(0);
    if(!isset($_SESSION)){
      session_start();
    }
  }
  function make(){
    $this->_init();
    $this->_createSlide();
    $this->_createBg();
    $this->_merge();
    $this->_imgout();
    $this->_destroy();
  }

  function check($offset=''){
    if(!$_SESSION['tncode_r']){
      return false;
    }
    if(!$offset){
      $offset = $_REQUEST['tn_r'];
    }
    $ret = abs($_SESSION['tncode_r']-$offset)<=$this->_fault;
    if($ret){
      unset($_SESSION['tncode_r']);
    }else{
      $_SESSION['tncode_err']++;
      if($_SESSION['tncode_err']>10){//错误10次必须刷新
        unset($_SESSION['tncode_r']);
      }
    }
    return $ret;
  }

  private function _init(){
    $bg = mt_rand(1,$this->bg_num);
    $file_bg = dirname(__FILE__).'/bg/'.$bg.'.png';
    $this->im_fullbg = imagecreatefrompng($file_bg);
    $this->im_bg = imagecreatetruecolor($this->bg_width, $this->bg_height);
    imagecopy($this->im_bg,$this->im_fullbg,0,0,0,0,$this->bg_width, $this->bg_height);
    $this->im_slide = imagecreatetruecolor($this->mark_width, $this->bg_height);
    $_SESSION['tncode_r'] = $this->_x = mt_rand(50,$this->bg_width-$this->mark_width-1);
    $_SESSION['tncode_err'] = 0;
    $this->_y = mt_rand(0,$this->bg_height-$this->mark_height-1);
  }

  private function _destroy(){
    imagedestroy($this->im);
    imagedestroy($this->im_fullbg);
    imagedestroy($this->im_bg);
    imagedestroy($this->im_slide);
  }
  private function _imgout(){
    if(!$_GET['nowebp']&&function_exists('imagewebp')){//优先webp格式,超高压缩率
      $type = 'webp';
      $quality = 40;//图片质量 0-100
    }else{
      $type = 'png';
      $quality = 7;//图片质量 0-9
    }
    header('Content-Type: image/'.$type);
    $func = "image".$type;
    $func($this->im,null,$quality);
  }
  private function _merge(){
    $this->im = imagecreatetruecolor($this->bg_width, $this->bg_height*3);
    imagecopy($this->im, $this->im_bg,0, 0 , 0, 0, $this->bg_width, $this->bg_height);
    imagecopy($this->im, $this->im_slide,0, $this->bg_height , 0, 0, $this->mark_width, $this->bg_height);
    imagecopy($this->im, $this->im_fullbg,0, $this->bg_height*2 , 0, 0, $this->bg_width, $this->bg_height);
    imagecolortransparent($this->im,0);//16777215
  }

  private function _createBg(){
    $file_mark = dirname(__FILE__).'/img/mark.png';
    $im = imagecreatefrompng($file_mark);
    header('Content-Type: image/png');
    //imagealphablending( $im, true);
    imagecolortransparent($im,0);//16777215
    //imagepng($im);exit;
    imagecopy($this->im_bg, $im, $this->_x, $this->_y , 0 , 0 , $this->mark_width, $this->mark_height);
    imagedestroy($im);
  }

  private function _createSlide(){
    $file_mark = dirname(__FILE__).'/img/mark2.png';
    $img_mark = imagecreatefrompng($file_mark);
    imagecopy($this->im_slide, $this->im_fullbg,0, $this->_y , $this->_x, $this->_y, $this->mark_width, $this->mark_height);
    imagecopy($this->im_slide, $img_mark,0, $this->_y , 0, 0, $this->mark_width, $this->mark_height);
    imagecolortransparent($this->im_slide,0);//16777215
    //header('Content-Type: image/png');
    //imagepng($this->im_slide);exit;
    imagedestroy($img_mark);
  }

}
?>

附:完整实例代码点击此处本站下载

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
ThinkPHP php 框架学习笔记
Oct 30 PHP
一些PHP Coding Tips(php小技巧)[2011/04/02最后更新]
May 02 PHP
php图像处理函数大全(推荐收藏)
Jul 11 PHP
PHP延迟静态绑定示例分享
Jun 22 PHP
如何让CI框架支持service层
Oct 29 PHP
PHP 实现代码复用的一个方法 traits新特性
Feb 22 PHP
详解WordPress中用于更新和获取用户选项数据的PHP函数
Mar 08 PHP
Laravel中使用FormRequest进行表单验证方法及问题汇总
Jun 19 PHP
php获取flash尺寸详细数据的方法
Nov 12 PHP
redis查看连接数及php模拟并发创建redis连接的方法
Dec 15 PHP
ThinkPHP 3.2.3实现加减乘除图片验证码
Dec 05 PHP
在laravel中实现事务回滚的方法
Oct 10 PHP
PHP code 验证码生成类定义和简单使用示例
May 27 #PHP
PHP 计算至少是其他数字两倍的最大数的实现代码
May 26 #PHP
tp5.1 框架数据库-数据集操作实例分析
May 26 #PHP
tp5.1 框架路由操作-URL生成实例分析
May 26 #PHP
tp5.1 框架join方法用法实例分析
May 26 #PHP
tp5.1框架数据库子查询操作实例分析
May 26 #PHP
tp5.1 框架数据库常见操作详解【添加、删除、更新、查询】
May 26 #PHP
You might like
压力如何影响浓缩咖啡品质
2021/03/03 咖啡文化
php之Memcache学习笔记
2013/06/17 PHP
关于URL最大长度限制的相关资料查证
2014/12/23 PHP
smarty内置函数{loteral}、{ldelim}和{rdelim}用法实例
2015/01/22 PHP
php实现的Curl封装类Curl.class.php用法实例分析
2015/09/25 PHP
PHP实现的多文件上传类及用法示例
2016/05/06 PHP
PHP实现数组根据某个单元字段排序操作示例
2018/08/01 PHP
PHP设计模式之委托模式定义与用法简单示例
2018/08/13 PHP
PHP rsa加密解密算法原理解析
2020/12/09 PHP
JAVASCRIPT 对象的创建与使用
2021/03/09 Javascript
iframe调用父页面函数示例详解
2014/07/17 Javascript
JS或jQuery获取ASP.NET服务器控件ID的方法
2015/06/08 Javascript
用JS生成UUID的方法实例
2016/03/30 Javascript
JavaScript 监控微信浏览器且自带返回按钮时间
2016/11/27 Javascript
BootStrap数据表格实例代码
2017/09/13 Javascript
JS内部事件机制之单线程原理
2018/07/02 Javascript
JS+canvas画布实现炫酷的旋转星空效果示例
2019/02/13 Javascript
Layui弹出层 加载 做编辑页面的方法
2019/09/16 Javascript
vue分页插件的使用方法
2019/12/25 Javascript
javascript History对象原理解析
2020/02/17 Javascript
JavaScript实现移动端弹窗后禁止滚动
2020/05/25 Javascript
Python的ORM框架SQLObject入门实例
2014/04/28 Python
对pandas中apply函数的用法详解
2018/04/10 Python
pyspark.sql.DataFrame与pandas.DataFrame之间的相互转换实例
2018/08/02 Python
在Pycharm中使用GitHub的方法步骤
2019/06/13 Python
matplotlib命令与格式之tick坐标轴日期格式(设置日期主副刻度)
2019/08/06 Python
python3.7环境下安装Anaconda的教程图解
2019/09/10 Python
Python常用模块函数代码汇总解析
2020/08/31 Python
美国在线轮胎零售商:SimpleTire
2019/04/08 全球购物
如何在.net Winform里面显示PDF文档
2012/09/11 面试题
计算机应用专业毕业生求职信
2013/10/24 职场文书
2015毕业实习推荐信
2015/03/23 职场文书
薪资证明范本
2015/06/19 职场文书
迎国庆主题班会
2015/08/17 职场文书
《乌鸦喝水》教学反思
2016/02/19 职场文书
MySQL系列之九 mysql查询缓存及索引
2021/07/02 MySQL