Thinkphp 인증코드 구현(양식, ajax 구현 인증)_php 예시

WBOY
풀어 주다: 2016-08-17 13:02:37
원래의
1158명이 탐색했습니다.

두 가지 확인 코드 확인 구현, 하나는 양식 제출 버튼에서 직접 확인을 구현하는 것이고, 다른 하나는 Ajax를 사용하여 매개변수를 전달하여 확인을 구현하는 것입니다.

1. 양식 제출 버튼에서 직접 확인을 구현하고 컨트롤러 verifyController.class.php에 다음 코드를 작성합니다.

namespace Home\Controller;
use Think\Controller;
class VerifyController extends Controller {
public function index() {
$this->display();
}
public function checkLogin() {
$verify=new \Think\Verify();
$code=I('post.verify');//表单验证码
if($verify->check($code)){
$this->success('验证码正确');
}else{
$this->error('验证码错误');
}
}
public function verify()
{
// 实例化Verify对象
$verify = new \Think\Verify();
// 配置验证码参数
$verify->fontSize = 14; // 验证码字体大小
$verify->length = 4; // 验证码位数
$verify->imageH = 34; // 验证码高度
$verify->useImgBg = true; // 开启验证码背景
$verify->useNoise = false; // 关闭验证码干扰杂点
$verify->entry();
}
} 
로그인 후 복사

Verify/index.html 뷰의 코드는 다음과 같습니다.

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="{:U('verify/checkLogin')}" method="post">
<div class="form-group has-feedback">
<input type="text" name="verify" id="verify" placeholder="验证码" style="width:100px;" />
<span style="right:120px;"></span>
<img class="verify" src="{:U(verify)}" alt="验证码" onClick="this.src=this.src+'&#63;'+Math.random()" />
</div>
<div class="col-xs-4">
<button type="submit" >验证</button>
</div>
</form>
</body>
</html> 
로그인 후 복사

2. ajax를 사용하여 확인을 위한 매개변수를 전달합니다. 컨트롤러의 ValidController.class.php 코드는 다음과 같습니다.

namespace Home\Controller;
use Think\Controller;
class VerifyController extends Controller {
public function index() {
$this->display();
}
public function checkLogin() {
$verify=new \Think\Verify();
$code=$_POST['code'];//ajax验证码获取
if($verify->check($code)){
$this->ajaxReturn(1);
}else{
$this->ajaxReturn(0);
}
}
public function verify()
{
// 实例化Verify对象
$verify = new \Think\Verify();
// 配置验证码参数
$verify->fontSize = 14; // 验证码字体大小
$verify->length = 4; // 验证码位数
$verify->imageH = 34; // 验证码高度
$verify->useImgBg = true; // 开启验证码背景
$verify->useNoise = false; // 关闭验证码干扰杂点
$verify->entry();
}
} 
로그인 후 복사

Verify/index.html 뷰의 코드는 다음과 같습니다.

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="__JS__/jquery-2.1.0.min.js" ></script>
</head>
<body>
<form action="{:U('verify/checkLogin')}" method="post">
<div class="form-group has-feedback">
<input type="text" name="verify" id="verify" placeholder="验证码" style="width:100px;" />
<span style="right:120px;"></span>
<img class="verify" src="{:U(verify)}" alt="验证码" onClick="this.src=this.src+'&#63;'+Math.random()" />
</div>
<div class="col-xs-4">
<button type="button" id="ver">验证</button>
</div>
</form>
<script>
$(document).ready(function(){
/*ajax验证码*/
$("#ver").click(function(){
var code=$("#verify").val();//获取输入验证码
var url=$('form').attr('action');//获取表单action的值
$.ajax({
type:"post",
url:url,
data:{"code":code},
error:function(request){
alert("ajax错误");
},
success:function(data){
if(data){
alert("正确")
}else{
alert('错误')
}
}
});
});
});
</script>
</body>
</html> 
로그인 후 복사

두 번째 방법은 jquery.min.js 파일 다운로드 주소(http://www.jq22.com/jquery-info122)를 잊지 마세요

구성 파일 Common/conf/config.php에서 주소를 구성합니다:

return array( 
/*地址替换*/
'TMPL_PARSE_STRING'=>array( 
'__JS__'=>__ROOT__.'/Public/JS',
),
);
로그인 후 복사

위는 에디터가 소개한 thinkphp 인증 코드(form, ajax 사용 인증)의 구현입니다. 궁금한 사항이 있으시면 메시지를 남겨주시면 에디터가 답변해 드리겠습니다. 당신은 시간에. 또한 Script House 웹사이트를 지원해 주시는 모든 분들께 감사의 말씀을 전하고 싶습니다!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!