PHP code example of yuanlj-tea / click-captcha
1. Go to this page and download the library: Download yuanlj-tea/click-captcha library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yuanlj-tea / click-captcha example snippets
composer
$captcha = new \ClickCaptcha\Captcha();
$captcha->output();
$inline = $captcha->getInline();
echo "<img src='".$linlie."' />";
$captcha->getCode();
'providers' => [
// ...
\ClickCaptcha\ClickCaptchaServiceProvider::class,
],
'aliases' => [
// ...
'ClickCaptcha' => \ClickCaptcha\ClickCaptchaFacade::class,
],
use ClickCaptcha\Captcha;
public function getImage(Request $request, Captcha $captcha)
{
}
use ClickCaptcha;
public function getImageV1()
{
}
public function getImageV2()
{
$captcha = app('click_captcha');
}
/**
* 获取验证码信息
*/
public function getCaptcha()
{
$key = 'click:captcha:' . str_random(32);
$captcha = new Captcha();
$cacheMinutes = 30;
$inline = $captcha->getInline();
$data['captcha_key'] = $key;
$data['expired_at'] = time() + $cacheMinutes * 60;
$code = $captcha->getCode();
$data['code'] = $code;
Cache::put($key, $code, $cacheMinutes);
$data['image'] = $inline;
return AjaxResponse::success($data);
}
/**
* 校验验证码
* @param Request $request
*/
public function check(Request $request)
{
$captcha_key = $request->input('captcha_key', '');
$param = $request->input('data', []);
if (!Cache::has($captcha_key)) {
return AjaxResponse::fail('验证码已过期,请刷新后再试');
}
if (!is_array($param)) {
return AjaxResponse::fail('验证失败,参数错误');
}
$code = Cache::get($captcha_key);
$checkCode = array_column($code, 'scope');
$errKey = $captcha_key . '_error';
if (Cache::get($errKey) >= 3) {
Cache::forget($captcha_key);
Cache::forget($errKey);
return AjaxResponse::fail('错误次数过多,请刷新验证码后再试');
}
foreach ($checkCode as $k => $v) {
if (!isset($param[$k])) {
Cache::increment($errKey);
return AjaxResponse::fail('验证失败');
}
if (!isset($param[$k]['x']) || !isset($param[$k]['y'])) {
Cache::increment($errKey);
return AjaxResponse::fail('验证失败');
}
$x = $param[$k]['x'];
$y = $param[$k]['y'];
if (
!(
$x >= $v['x_limit_left'] && $x <= $v['x_limit_right'] &&
$y >= $v['y_limit_up'] && $y <= $v['y_limit_down']
)
) {
Cache::increment($errKey);
return AjaxResponse::fail('验证失败');
}
}
Cache::forget($errKey);
return AjaxResponse::success('验证成功');
}