這期內容當中小編將會給大家帶來有關使用YII2框架怎么實現一個驗證碼功能,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

首先我們在控制器里創建一個actions方法,用于使用yii\captcha\CaptchaAction
<?php
namespace app\controllers;
use YII;
use yii\web\Controller;
class IndexController extends Controller
{
public function actionIndex()
{
if (YII::$app->request->isPost) {
//獲取post過來的驗證碼
$verify = YII::$app->request->post('verify');
//我們手動進行驗證,第二個參數表示是否區分大小寫
if ($this->createAction('captcha')->validate($verify, false)) {
echo '成功';
} else {
echo '失敗';
}
} else {
return $this->renderPartial('index');
}
}
//actions的作用主要是共用功能相同的方法
//當用戶訪問index/captcha時,actions就會調用yii\captcha\CaptchaAction方法
public function actions()
{
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => null,
//背景顏色
'backColor' => 0x000000,
//較大顯示個數
'maxLength' => 4,
//最少顯示個數
'minLength' => 4,
//間距
'padding' => 2,
//高度
'height' => 30,
//寬度
'width' => 85,
//字體顏色
'foreColor' => 0xffffff,
//設置字符偏移量
'offset' => 4,
],
];
}
}顯示頁面代碼如下:
<?php
use yii\helpers\Url;
use yii\helpers\Html;
?>
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>分頁顯示</title>
</head>
<body>
<form action="<?php echo Url::toRoute('index/index'); ?>" method="post">
驗證碼:<input type="text" name="verify"><br>
<img id="verifyImg" src="<?php echo Url::toRoute('index/captcha'); ?>"><br>
<input type="submit" value="提交">
<input name="_csrf" type="hidden" value="<?php echo \Yii::$app->request->csrfToken; ?>">
</form>
<?php echo Html::jsFile('@web/js/jquery-3.3.1.min.js'); ?>
<script type="text/javascript">
$(function () {
//處理點擊刷新驗證碼
$("#verifyImg").on("click", function () {
$.get("<?php echo Url::toRoute('index/captcha') ?>?refresh", function (data) {
$("#verifyImg").attr("src", data["url"]);
}, "json");
});
});
</script>
</body>
</html>演示結果如下:

上面控制器中驗證碼的驗證方式是我們手動的。我們也可以創建一個模型配置rules()來自動完成。
<?php
namespace app\models;
use yii\base\Model;
class VerifyForm extends Model
{
//變量名為你表單中輸入驗證碼控件的name
public $verify;
public function rules()
{
return [
['verify', 'required', 'message' => '請填寫驗證碼'],
//注意captchaAction的設置,指向你顯示驗證碼的action,這里我們的是index/captcha
['verify', 'captcha', 'captchaAction' => 'index/captcha', 'caseSensitive' => false, 'message' => '驗證碼錯誤'],
];
}
}控制器代碼修改如下:
<?php
namespace app\controllers;
use YII;
use app\models\VerifyForm;
use yii\web\Controller;
class IndexController extends Controller
{
public function actionIndex()
{
if (YII::$app->request->isPost) {
$verify = new VerifyForm();
$verify->load(YII::$app->request->post(), '');
//自動驗證
if ($verify->validate()) {
echo '成功';
} else {
var_dump($verify->errors);
}
} else {
return $this->renderPartial('index');
}
}
//actions的作用主要是共用功能相同的方法
//當用戶訪問index/captcha時,actions就會調用yii\captcha\CaptchaAction方法
public function actions()
{
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => null,
//背景顏色
'backColor' => 0x000000,
//較大顯示個數
'maxLength' => 4,
//最少顯示個數
'minLength' => 4,
//間距
'padding' => 2,
//高度
'height' => 30,
//寬度
'width' => 85,
//字體顏色
'foreColor' => 0xffffff,
//設置字符偏移量
'offset' => 4,
],
];
}
}上述就是小編為大家分享的使用YII2框架怎么實現一個驗證碼功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創新互聯行業資訊頻道。
網頁題目:使用YII2框架怎么實現一個驗證碼功能-創新互聯
網頁鏈接:http://www.yijiale78.com/article0/pscio.html
成都網站建設公司_創新互聯,為您提供關鍵詞優化、網站設計、做網站、網頁設計公司、全網營銷推廣、ChatGPT
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯