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 相关文章推荐
关于在php.ini中添加extension=php_mysqli.dll指令的说明
Jun 14 PHP
php Ajax乱码
Apr 09 PHP
PHP学习笔记 用户注册模块用户类以及验证码类
Sep 20 PHP
供参考的 php 学习提高路线分享
Oct 23 PHP
采用ThinkPHP中F方法实现快速缓存实例
Jun 13 PHP
phpword插件导出word文件时中文乱码问题处理方案
Aug 19 PHP
thinkphp autoload 命名空间自定义 namespace
Jul 17 PHP
PHP使用Pthread实现的多线程操作实例
Nov 14 PHP
PHP数据库操作二:memcache用法分析
Aug 16 PHP
PHP异常类及异常处理操作实例详解
Dec 19 PHP
Thinkphp5.0 框架使用模型Model添加、更新、删除数据操作详解
Oct 11 PHP
PHP7原生MySQL数据库操作实现代码
Jul 03 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
Ajax和PHP正则表达式验证表单及验证码
2016/09/24 PHP
php数据库操作model类(使用__call方法)
2016/11/16 PHP
srcElement表格样式
2006/09/03 Javascript
YUI 读码日记之 YAHOO.lang.is*
2008/03/22 Javascript
javascript函数重载解决方案分享
2014/02/19 Javascript
js库Modernizr的介绍和使用
2015/05/07 Javascript
JavaScript中split与join函数的进阶使用技巧
2016/05/03 Javascript
预防网页挂马的方法总结
2016/11/03 Javascript
jacascript DOM节点——元素节点、属性节点、文本节点
2017/04/18 Javascript
初识 Vue.js 中的 *.Vue文件
2017/11/22 Javascript
Python编程之变量赋值操作实例分析
2017/07/24 Python
Python针对给定列表中元素进行翻转操作的方法分析
2018/04/27 Python
python求解数组中两个字符串的最小距离
2018/09/27 Python
Python2和Python3中urllib库中urlencode的使用注意事项
2018/11/26 Python
Python操作Excel插入删除行的方法
2018/12/10 Python
使用python进行广告点击率的预测的实现
2019/07/04 Python
Python 生成器,迭代,yield关键字,send()传参给yield语句操作示例
2019/10/12 Python
基于Python生成个性二维码过程详解
2020/03/05 Python
Python实现密钥密码(加解密)实例详解
2020/04/26 Python
将pymysql获取到的数据类型是tuple转化为pandas方式
2020/05/15 Python
Elasticsearch py客户端库安装及使用方法解析
2020/09/14 Python
HTML5 Canvas绘制圆点虚线实例
2015/01/01 HTML / CSS
字中字效果的实现【html5实例】
2016/05/03 HTML / CSS
基于 HTML5 WebGL 实现的医疗物流系统
2019/10/08 HTML / CSS
Cole Haan官方网站:美国时尚潮流品牌
2017/12/06 全球购物
半年思想汇报
2013/12/30 职场文书
大学生职业生涯规划书模板
2014/01/03 职场文书
自荐书范文范例
2014/02/13 职场文书
2014年党的群众路线教育实践活动总结
2014/04/25 职场文书
计算机求职信
2014/07/02 职场文书
父亲节活动策划方案
2014/08/24 职场文书
办公室主任四风问题对照检查材料思想汇报
2014/09/28 职场文书
个人房屋转让协议书范本
2014/10/26 职场文书
党的群众路线调研报告
2014/11/03 职场文书
微信小程序和php的登录实现
2021/04/01 PHP
Android Flutter实现3D动画效果示例详解
2022/04/07 Java/Android