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: 2024-09-28 19:20:44 UTC
README
安装
composer require ga/captcha dev-master
用法
1. 在 SiteController
的 actions
方法中设置以下代码。(默认) 您也可以更改“url”路由,但您应该在下一步中更改 CaptchaWidget 的 captchaAction
属性。
Class SiteController extend yii\web\Controller public function actions() { return [ 'captcha'=>[ 'class' => 'ga\captcha\CaptchaAction', ] ]; }
2. 在视图文件中创建 Widget
,并支持激活模式。
<?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()?>
注意: 如果您更改了url路由,您需要设置 captchaAction
属性。
<?= CaptchaWidget::widget([ ..., 'captchaAction'=> 'controller/action' ]);?>
3. 配置您的需要验证验证码的方法。
public function actionTest(){ $res = Yii::$app->request->post('captcha'); $ca = new CaptchaValidator(); if($ca->validate($res)){ echo "success"; }else{ echo "fail"; } }
其他
感谢 HansKendrickV-Regular.ttf
的作者提供优秀的免费字体。