console
var handlerEmbed = function(captchaObj) {
$("#embed-submit").click(function(e) {
var validate = captchaObj.getValidate();
if (!validate) {
$("#notice")[0].className = "show";
setTimeout(function() {
$("#notice")[0].className = "hide";
},
2000);
e.preventDefault();
}
});
captchaObj.appendTo("#embed-captcha");
captchaObj.onReady(function() {
$("#wait")[0].className = "hide";
});
};
$.ajax({
url: "{:U('Jiyan/StartCaptchaServlet')}?t=" + (new Date()).getTime(),
type: "get",
dataType: "json",
success: function(data) {
initGeetest({
gt: data.gt,
challenge: data.challenge,
product: "embed",
offline: !data.success
},
handlerEmbed);
}
});
<php> namespace Api\Controller; use Think\Controller; class JiyanController
extends Controller { /* 这里id和key是测试的 在用户管理后台,有这两个参数,只要填上去即可。 */ private
$id; private $key; private $GtSdk; public function _initialize() { $this->
id = "b46d1900d0a894591916ea94ea91bd2c"; $this->key = "36fc3fe98530eea08dfc6ce76e3d24c4";
$this->GtSdk = new \Org\Util\GeetestLib($this->id, $this->key); } public
function index() { $this->display(); } /** * 使用Get的方式返回:challenge和capthca_id
此方式以实现前后端完全分离的开发模式 专门实现failback * @author Tanxu */ //error_reporting(0);
public function StartCaptchaServlet() { session_start(); $user_id = "test";
$status = $this->GtSdk->pre_process($user_id); $_SESSION['gtserver'] =
$status; $_SESSION['user_id'] = $user_id; echo $this->GtSdk->get_response_str();
} /** * 输出二次验证结果,本文件示例只是简单的输出 Yes or No */ // error_reporting(0); public
function VerifyLoginServlet() { $user_id = $_SESSION['user_id']; if ($_SESSION['gtserver']
== 1) { $result = $this->GtSdk->success_validate($_POST['geetest_challenge'],
$_POST['geetest_validate'], $_POST['geetest_seccode'], $user_id); if ($result)
{ echo 'Yes!'; } else{ echo 'No'; } }else{ if ($this->GtSdk->fail_validate($_POST['geetest_challenge'],$_POST['geetest_validate'],$_POST['geetest_seccode']))
{ echo "yes"; }else{ echo "no"; } } } } </php>
<form class="popup" action="" method="post">
<h2>
嵌入式Demo,使用表单形式提交二次验证所需的验证结果值
</h2>
<br>
<p>
<label for="user">
用户名:
</label>
<input class="inp" id="user" type="text" value="极验验证">
</p>
<br>
<p>
<label for="password">
密 码:
</label>
<input class="inp" id="password" type="password" value="123456">
</p>
<div id="embed-captcha">
</div>
<p id="wait" class="show">
正在加载验证码......
</p>
<p id="notice" class="hide">
请先拖动验证码到相应位置
</p>
<br>
<input class="btn" id="embed-submit" type="submit" value="提交">
</form>