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

 更新时间:2025年08月20日 09:53:31   作者:下页、再停留  
这篇文章主要介绍了php+js实现的拖动滑块验证码验证表单操作,结合实例形式分析了php+js拖动滑块验证码验证表单操作基本功能实现与使用相关操作技巧,需要的朋友可以参考下

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

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

极验拖动动画图

代码文件截图

代码实现

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数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

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

相关文章

  • php 引用(&)详解

    php 引用(&)详解

    php的引用(就是在变量或者函数、对象等前面加上&符号)
    2009-11-11
  • php实现统计网站在线人数的方法

    php实现统计网站在线人数的方法

    这篇文章主要介绍了php实现统计网站在线人数的方法,通过获取服务器端网络参数及文本文件读写实现统计在线人数的功能,非常简单实用,需要的朋友可以参考下
    2015-05-05
  • PHP解析RuoYi框架实现Token解密详解

    PHP解析RuoYi框架实现Token解密详解

    这篇文章主要介绍了PHP解析RuoYi框架实现Token解密,同时本篇文章对RuoYi若依框架的使用进行简单的介绍,它和php的fastadmin框架非常类似,是可以根据数据库表自动的生成一个完整的管理后台
    2022-10-10
  • PHP按指定键值对二维数组进行排序的方法

    PHP按指定键值对二维数组进行排序的方法

    这篇文章主要介绍了PHP按指定键值对二维数组进行排序的方法,涉及PHP二维数组的遍历及array_multisort函数的使用技巧,需要的朋友可以参考下
    2015-12-12
  • php在linux下检测mysql同步状态的方法

    php在linux下检测mysql同步状态的方法

    这篇文章主要介绍了php在linux下检测mysql同步状态的方法,是Linux下使用php检测mysql同步状态的实用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • php中使用Imagick实现图像直方图的实现代码

    php中使用Imagick实现图像直方图的实现代码

    玩过单反相机的人应该都知道图像直方图(Image Histogram),简单点说,它通过计算每个色阶在总像素中所占的比例来反映图像的曝光情况。
    2011-08-08
  • PHP IN_ARRAY 函数使用注意事项

    PHP IN_ARRAY 函数使用注意事项

    其实关键还是因为 php是弱类型语言,php进行比较的时候 最好还是使用strict方法的。因为这样不但比较两者的值是否一直,还会比较两者的类型是否一直。
    2010-07-07
  • PHP超级全局变量数组小结

    PHP超级全局变量数组小结

    PHP超级全局变量数组(Super Global Array),又称为PHP预定义数组,是由PHP引擎内置的,不需要开发者重新定义。 在PHP脚本运行时,PHP会自动将一些数据放在超级全局数组中
    2012-10-10
  • php控制文件下载速度的方法

    php控制文件下载速度的方法

    这篇文章主要介绍了php控制文件下载速度的方法,实例分析了php操作文件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • php设计模式之装饰模式应用案例详解

    php设计模式之装饰模式应用案例详解

    这篇文章主要介绍了php设计模式之装饰模式,结合具体应用案例形式详细分析了php装饰模式的概念、原理、用法及相关操作注意事项,需要的朋友可以参考下
    2019-06-06

最新评论