±¾À´ÒÔΪyii2¿o¼ÜÑeÖ¤ÂeÕa¿eºÜÈ«Ãae£¬³¢ÊÔ°Ù¶ÈgoogleÁËһϣ¬´o¶aÊý½Ì³ÌдµÄÁaÁaɢɢ²»È«Ãae£¬Ïe×Å×Ô¼ºÐ´Ò»*Ý´øÓÐÍeÕu²½ÖeµÄÑeÖ¤Âe½Ì³Ì¡£
ÎÒÃǼÙÉesite/login ±iµ¥µÇ¼ÐeÒªÔo¼ÓÑeÖ¤Âe¡£
1¡¢siteController¿ØÖÆÆ÷µÄactions½¨Ôo¼ÓcaptchaÉeÖÃ
public function actions() {
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'maxLength' => 4,
'minLength' => 4
],
];
}
ÉÏÃaeÎÒÃǼoµ¥ÉeÖÃÁËÑeÖ¤ÂeµÄλÊý£¬ÓÐС»i°eºÃÆae¶¼ÓÐÄÄÐ(C)ÅaÖÃÏi£¬Õa¸oÄa¿ÉÒÔ²e¿´Îļþ vendor\yiisoft\yii2\captcha£¬°uÀ¨ÑeÖ¤Âe±³¾°É«£¬×ÖÌaÎļþµÈÉeÖö¼¿ÉÒÔÔÚÕaÀiÕÒµ½¡£
2¡¢siteController¼ÌÐøÅaÖá£
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error', 'captcha'],
'allow' => true,
],
],
];
}
Ϊaccess rulesµÄactionsÔo¼Ócaptcha½¨¿É*ÃÎÊ¡£
3¡¢ÎÒÃÇ¿´¿´view²a£¬Ôo¼ÓÑeÖ¤Âeinput¡£
use yii\captcha\Captcha;
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
]) ?>
4¡¢ÕaÑu»¹²»ÐУ¬ÎÒÃÇ»¹ÐeÒªÔo¼ÓÑeÖ¤ÂeµÄÑeÖ¤¹aeÔo
ÎÒÃÇÕaÀiÓõ½µÄÊÇLoginForm£¬Òo´ËÐÞ¸ÄLoginFormÎļþ
class LoginForm extends Model {
//...... public $verifyCode;
public function rules() {
return [
//......
['verifyCode', 'captcha'],
];
}
public function attributeLabels() {
return [
'verifyCode' => '', //ÑeÖ¤ÂeµÄÃu³Æ£¬¸u¾Ý¸oÈËϲºÃÉe¶¨
];
}
}
//¶¨ÒaÁËverifyCodeÊoÐÔ
//rules¹aeÔoÌi¼ÓÁËÑeÖ¤
//labelÖж¨ÒaÁËÆaÏÔʾÃu³Æ
5¡¢µ½µÚËIJ½»u±¾ÉÏÅaÖúÃÑeÖ¤Âe¾Í»aÕý³£ÏÔʾÁË¡£Èç¹uÄaµÄºǫÉe¶¨ÁËrbacȨÏÞ¿ØÖÆ£¬¿ÖÅÂÄaÈÔÈ»ÐeÒªÔÚconfigÄÚΪas accssÔo¼Ó/site/captcha¿É*ÃÎÊ¡£
6¡¢¿´Ð§¹u¾ÍºÃÁË¡£
7¡¢ÓÐͬѧÎÊΪɶҳÃaeË¢ÐÂÑeÖ¤Âe²»¸u×ÅˢУ¬ÎÒ¸oÈ˾oµÃË¢²»Ë¢Ð²»ÖØÒª£¬µ±ÄaÊa´iÑeÖ¤ÂeÒ³ÃaeˢеÄʱºoÑeÖ¤Âe²Å»aˢС£Èç¹uÄaÇҪˢÐÂÒ³ÃaeÑeÖ¤Âe¸u×ÅˢУ¬³¢ÊÔÒ»ÖÖ¼oµ¥µÄ½*¨ÊµÏÖ¡£
$('ÑeÖ¤Âe¶ÔÏo').click();
¼´ÔÚÒ³ÃaeˢеÄʱºoÖØеa»÷Ò»´ÎÑeÖ¤Âe½øÐÐÇ¿ÖÆˢС£
ÒÔÉÏËuÊoÊÇС±a¸ø´o¼Ò½eÉܵÄYii2Ôo¼ÓÑeÖ¤Âe²½Öe£¬Ï£Íu¶Ô´o¼ÒÓÐËu°iÖu£¡
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8