ga / captcha
yii2-captcha
dev-master
2017-01-20 03:40 UTC
Requires
- bower-asset/jquery: 2.2.*@stable | 2.1.*@stable | 1.11.*@stable | 1.12.*@stable
This package is not auto-updated.
Last update: 2025-02-15 21:48:58 UTC
README
Installation
composer require ga/captcha dev-master
Usage
1.Set up following code in actions
method of SiteController
.(Default)
You can also change the "url" route what you want, but you should change the captchaAction
property for CaptchaWidget in the next step.
Class SiteController extend yii\web\Controller public function actions() { return [ 'captcha'=>[ 'class' => 'ga\captcha\CaptchaAction', ] ]; }
2.Create Widget
in the view file, and it's support the active mode.
<?php use app\models\CaptchaWidget; use yii\helpers\Html; ?> <?= Html::beginForm('site/test', 'post')?> <?= CaptchaWidget::widget([ 'name' => 'captcha', 'template' => '<label for="captcha">Captcha</label>  {input}{image}', 'options' => ['id' => 'captcha'], ]);?> <?= Html::submitButton('Submit')?> <?= Html::endForm()?>
NOTE: If you change the url route, and you need to set the captchaAction
property.
<?= CaptchaWidget::widget([ ..., 'captchaAction'=> 'controller/action' ]);?>
3.Config your method need to validate the verification code.
public function actionTest(){ $res = Yii::$app->request->post('captcha'); $ca = new CaptchaValidator(); if($ca->validate($res)){ echo "success"; }else{ echo "fail"; } }
Others
Thanks for the author of HansKendrickV-Regular.ttf
to provide the nice free font.