//配置项
<?php
return [
//验证码
'CODE_ID' => '63230acdc3b8dfaf53645a27967c72d1',
'CODE_KEY' => '088e11955b70908551f64b9aeac4f68f',
];
?>
//控制器
public function actionCode()
{
$data = [];
$code_id = Yii::$app->params['CODE_ID'];
$code_key = Yii::$app->params['CODE_KEY'];
$geetest = new Geetest($code_id, $code_key);
$user_id = "test";
$status = $geetest->pre_process($user_id);
//这里是验证是否通过验证
if (Yii::$app->request->isPost) {
$geetest_data = Yii::$app->session['geetest'];
$user_id = $geetest_data['user_id'];
$code_data = Yii::$app->request->post();
if ($geetest_data['gtserver'] == 1) {
$result = $geetest->success_validate($code_data['geetest_challenge'], $code_data['geetest_validate'], $code_data['geetest_seccode'], $user_id);
if ($result) {
echo '验证成功';
} else {
echo '验证失败';
}
} else {
if ($geetest->fail_validate($code_data['geetest_challenge'], $code_data['geetest_validate'], $code_data['geetest_seccode'])) {
echo '验证成功';
} else {
echo '验证失败';
}
}
}
//存入session
Yii::$app->session['geetest'] = [
'gtserver' => $status,
'user_id' => $user_id
];
$data['get_response_str'] = json_decode($geetest->get_response_str(), true);
return $this->renderPartial('code1', $data);
}