您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關如何使用php+js實現極驗,拖動滑塊驗證碼驗證表單的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
代碼實現
html文件
<!DOCTYPEhtml>
<htmllang="">
<head>
<metacharset="utf-8">
<metahttp-equiv="x-ua-compatible"content="ie=edge">
<metaname="viewport"content="width=device-width,initial-scale=1">
<title>極驗滑塊拖動驗證碼-碼農社區-web視頻分享網</title>
<scripttype="text/javascript"src="tn_code.js?v=35"></script>
<linkrel="stylesheet"type="text/css"href="style.css?v=27"/>
<styletype="text/css"></style>
</head>
<bodystyle="text-align:center;">
<divclass="tncode"style="text-align:center;margin:100pxauto;"></div>
<scripttype="text/javascript">
$TN.onsuccess(function(){
//驗證通過
});
</script>
php文件:check.php
<?php
require_oncedirname(__FILE__).'/TnCode.class.php';
$tn=newTnCode();
if($tn->check()){
$_SESSION['tncode_check']='ok';
echo"ok";
}else{
$_SESSION['tncode_check']='error';
echo"error";
}
?>
主要核心文件:TnCode.class.php
<?php
classTnCode
{
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();
}
}
functionmake(){
$this->_init();
$this->_createSlide();
$this->_createBg();
$this->_merge();
$this->_imgout();
$this->_destroy();
}
functioncheck($offset=''){
if(!$_SESSION['tncode_r']){
returnfalse;
}
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;
}
privatefunction_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);
}
privatefunction_destroy(){
imagedestroy($this->im);
imagedestroy($this->im_fullbg);
imagedestroy($this->im_bg);
imagedestroy($this->im_slide);
}
privatefunction_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);
}
privatefunction_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
}
privatefunction_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);
}
privatefunction_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+js實現極驗,拖動滑塊驗證碼驗證表單”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。