《PHP實(shí)例:Yii 2.0自帶的驗(yàn)證碼使用經(jīng)驗(yàn)分享》要點(diǎn):
本文介紹了PHP實(shí)例:Yii 2.0自帶的驗(yàn)證碼使用經(jīng)驗(yàn)分享,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:YII框架
PHP應(yīng)用前言
PHP應(yīng)用官網(wǎng)自帶的前臺(tái)驗(yàn)證碼中在view下有個(gè)contact.php的 文件,大家沒事可以先看看它是怎么調(diào)驗(yàn)證碼 閑話不說,感興趣的朋友們下面來一起看看詳細(xì)的介紹:
PHP應(yīng)用使用方法如下:
PHP應(yīng)用第一步: 因?yàn)槲冶旧斫⒘薽odules,所以我在我的modules下新建了models的目錄(默認(rèn)gii生成modules是沒有這個(gè)目錄的),我取名為L(zhǎng)oginForm.php
PHP應(yīng)用代碼 如下:
PHP應(yīng)用
namespace app\modules\XXX\models;//這個(gè)你們寫自己的命名空間,我以我的modules項(xiàng)目路徑為例
use Yii;
use yii\base\Model;
use yii\captcha\Captcha;
class LoginForm extends Model
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;//驗(yàn)證碼這個(gè)變量是必須建的,因?yàn)橐獌?chǔ)存驗(yàn)證碼的值` /** * @return array the validation rules. */
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email
['email', 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha'],//注意這里,在百度中查到很多教程,這里寫的都不一樣,最 簡(jiǎn)單的寫法就像我這種寫法,當(dāng)然還有其它各種寫法
//['verifyCode', 'captcha','captchaAction'=>'admin/index/captcha','message'=>'驗(yàn) 證碼不正確!'], 這種寫法在官網(wǎng)自帶的LoginForm.php中有寫到,大家可以沒事看看 ];
}
/*
* * @return array customized attribute labels
*/
public function attributeLabels()
{
return [
// 'verifyCode' => 'Verification Code',
'verifyCode' => '',//在官網(wǎng)的教程里是加上了英文字母,我這里先給去掉了,這里去 掉會(huì)不會(huì)產(chǎn)生影響因?yàn)槲疫€沒做接收驗(yàn)證,只做了驗(yàn)證碼顯示的功能,你們可以自己測(cè)試下
];
}
/***/
PHP應(yīng)用然后第二步我們?nèi)タ刂破骼锛尤氪a
PHP應(yīng)用
namespace app\modules\XXX\controllers;//你們自己的控制器空間
use yii\web\Controller;
use yii\web\Session;
use Yii;
use app\modules\XXX\models\LoginForm;//XXX你們自己定義的名字
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
/*
*這個(gè)是對(duì)應(yīng)前臺(tái)模版的action
*/
public function actionLogin()
{
$loginForm = new LoginForm();//這里要把剛才寫的類new下,注意你們要引入文件路徑額
$this->render('login',array('loginForm'=>$loginForm));//變量傳到前臺(tái)模版
}
/**
* @用戶授權(quán)規(guī)則
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout', 'signup','login'],//這里一定要加
'rules' => [
[
'actions' => ['login','captcha'],
'allow' => true,
'roles' => ['?'],
],
[
'actions'=>['logout','edit','add','del','index','users','thumb','upload','cutpic','follow','nofollow'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* @驗(yàn)證碼獨(dú)立操作 下面這個(gè)actions注意一點(diǎn),驗(yàn)證碼調(diào)試出來的樣式也許你并不滿意,這里就可
以需修改,這些個(gè)參數(shù)對(duì)應(yīng)的類是@app\vendor\yiisoft\yii2\captcha\CaptchaAction.php,可以參照這個(gè)
類里的參數(shù)去修改,也可以直接修改這個(gè)類的默認(rèn)參數(shù),這樣這里就不需要改了
*/
public function actions()
{
return [
// 'captcha' =>
// [
// 'class' => 'yii\captcha\CaptchaAction',
// 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
// ], //默認(rèn)的寫法
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
'backColor'=>0x000000,//背景顏色
'maxLength' => 6, //最大顯示個(gè)數(shù)
'minLength' => 5,//最少顯示個(gè)數(shù)
'padding' => 5,//間距
'height'=>40,//高度
'width' => 130, //寬度
'foreColor'=>0xffffff, //字體顏色
'offset'=>4, //設(shè)置字符偏移量 有效果
//'controller'=>'login', //擁有這個(gè)動(dòng)作的controller
],
];
}
PHP應(yīng)用到這里第二步 控制器的代碼就完成了,其中要加入的類,你們自己要留意,別落下!
PHP應(yīng)用第三步:
PHP應(yīng)用在view的模版里,我這里是login.php加入以下代碼
PHP應(yīng)用
<?php
$form = ActiveForm::begin([
'id' => 'login-form',
]);
?>
<?php
echo Captcha::widget(['name'=>'captchaimg','captchaAction'=>'login/captcha','imageOptions'=>['id'=>'captchaimg', 'title'=>'換一個(gè)', 'alt'=>'換一個(gè)', 'style'=>'cursor:pointer;margin-left:25px;'],'template'=>'{image}']);//我這里寫的跟官方的不一樣,因?yàn)槲疫@里加了一個(gè)參數(shù)(login/captcha),這個(gè)參數(shù)指向你當(dāng)前控制器名,如果不加這句,就會(huì)找到默認(rèn)的site控制器上去,驗(yàn)證碼會(huì)一直出不來,在style里是可以寫css代碼的,可以調(diào)試樣式 ?>
<?php
ActiveForm::end();
?>
PHP應(yīng)用總結(jié)
PHP應(yīng)用以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)維易PHP的支持.
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/608.html