Watch Star Fork

东城影 / GACaptchaPHPMIT

GACaptcha is a captcha component for Yii2.
克隆/下载
README.md 1.49 KB

GACaptcha(best,easy)

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>&emsp;&emsp;{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.

项目点评 ( 0 )

你可以在登录后,对此项目发表评论