検索
ホームページバックエンド開発PHPチュートリアルyii 検証コードの使用と検証プロセス

yii 検証コードの使用と検証プロセス

このプロセスを実装するには、いくつかの手順が必要です

最初のステップはコントローラーの操作です

操作対象のコントローラー 次のコードを追加します。

public function actions(){return array( // captcha action renders the CAPTCHA image displayed on the contact page'captcha'=>array('class'=>'CCaptchaAction','backColor'=>0xFFFFFF, 'maxLength'=>'8', // 最多生成几个字符'minLength'=>'7', // 最少生成几个字符'height'=>'40','width'=>'230',), ); }public function accessRules(){return array(array('allow','actions'=>array('captcha'),'users'=>array('*'),),);}

2 番目のステップは、ビューを操作することです

確認コードを表示したい:

<div class="control-group"><?php $this->widget('CCaptcha',array('showRefreshButton'=>true,'clickableImage'=>false,'buttonLabel'=>'刷新验证码','imageOptions'=>array('alt'=>'点击换图','title'=>'点击换图','style'=>'cursor:pointer','padding'=>'10'))); ?></div>


3 番目のステップは LoginForm の操作です

<?php/*** LoginForm class.* LoginForm is the data structure for keeping* user login form data. It is used by the 'login' action of 'SiteController'.*/class LoginForm extends CFormModel{public $username;public $password;public $rememberMe;public $verifyCode;private $_identity;/*** Declares the validation rules.* The rules state that username and password are required,* and password needs to be authenticated.*/public function rules(){return array(// username and password are required//	array('username, password', 'required'),array('username','required','message'=>'登录帐号不能为空'),array('password','required','message'=>'密码不能为空'),array('verifyCode','required','message'=>'验证码不能为空'),array('verifyCode','captcha', 'on'=>'login','allowEmpty'=>!Yii::app()->admin->isGuest),// rememberMe needs to be a booleanarray('rememberMe', 'boolean'),// password needs to be authenticatedarray('password', 'authenticate'),);}/*** Declares attribute labels.*/public function attributeLabels(){return array('rememberMe'=>'下次记住我','verifyCode' =>'验证码');}/*** Authenticates the password.* This is the 'authenticate' validator as declared in rules().*/public function authenticate($attribute,$params){if(!$this->hasErrors()){$this->_identity=new UserIdentity($this->username,$this->password);if(!$this->_identity->authenticate())$this->addError('password','帐号或密码错误.');}}public function validateVerifyCode($verifyCode){if(strtolower($this->verifyCode) === strtolower($verifyCode)){return true;}else{$this->addError('verifyCode','验证码错误.');}}/*** Logs in the user using the given username and password in the model.* @return boolean whether login is successful*/public function login(){if($this->_identity===null){$this->_identity=new UserIdentity($this->username,$this->password);$this->_identity->authenticate();}if($this->_identity->errorCode===UserIdentity::ERROR_NONE){$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 daysYii::app()->user->login($this->_identity,$duration);return true;}else{return false;}	}}

4 番目のステップ、検証プロセスを実装するために、私が作成したメソッドの 1 つを詳しく見てみましょう。
validateVerifyCode は、コントローラー

私の呼び出しは次のとおりです:

public function actionLogin(){$model=new LoginForm;if(isset($_POST['ajax']) && $_POST['ajax']==='login-form'){echo CActiveForm::validate($model);Yii::app()->end();}if(isset($_POST['LoginForm'])){$model->attributes=$_POST['LoginForm'];// validate user input and redirect to the previous page if validif($model->validate() && $model->validateVerifyCode($this->createAction('captcha')->getVerifyCode()) && $model->login()){$this->redirect(CController::createUrl('default/index'));}}$this->render('login',array('model'=>$model));}

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
PHP依存性噴射コンテナ:クイックスタートPHP依存性噴射コンテナ:クイックスタートMay 13, 2025 am 12:11 AM

aphpDependencyInjectionContaineriSATOULTAINATINAGECLASSDEPTINCIES、強化測定性、テスト可能性、および維持可能性。

PHPの依存噴射対サービスロケーターPHPの依存噴射対サービスロケーターMay 13, 2025 am 12:10 AM

SELECT DEPENTENCINGINOFCENT(DI)大規模なアプリケーションの場合、ServicElocatorは小さなプロジェクトまたはプロトタイプに適しています。 1)DIは、コンストラクターインジェクションを通じてコードのテスト可能性とモジュール性を改善します。 2)ServiceLocatorは、センター登録を通じてサービスを取得します。これは便利ですが、コードカップリングの増加につながる可能性があります。

PHPパフォーマンス最適化戦略。PHPパフォーマンス最適化戦略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedforspeedandEfficiencyby:1)enabingopcacheinphp.ini、2)PreparedStatementswithpordatabasequeriesを使用して、3)LoopswithArray_filterandarray_mapfordataprocessing、4)の構成ngincasaSearverseproxy、5)

PHPメールの検証:電子メールが正しく送信されるようにしますPHPメールの検証:電子メールが正しく送信されるようにしますMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

PHPアプリケーションをより速くする方法PHPアプリケーションをより速くする方法May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster、followthesesteps:1)useopcodecachinglikeopcacheTostoredscriptbytecode.2)最小化abasequeriesecachingingindexing.3)leveragephp7機能forbettercodeefficiency.4)

PHP依存性インジェクション:コードのテスト可能性を改善しますPHP依存性インジェクション:コードのテスト可能性を改善しますMay 12, 2025 am 12:03 AM

依存性注入(DI)は、明示的に推移的な依存関係によりPHPコードのテスト可能性を大幅に改善します。 1)DI分離クラスと特定の実装により、テストとメンテナンスが柔軟になります。 2)3つのタイプのうち、コンストラクターは、状態を一貫性に保つために明示的な式依存性を注入します。 3)DIコンテナを使用して複雑な依存関係を管理し、コードの品質と開発効率を向上させます。

PHPパフォーマンスの最適化:データベースクエリの最適化PHPパフォーマンスの最適化:データベースクエリの最適化May 12, 2025 am 12:02 AM

DatabaseQueryoptimizationInpholvesseveralstrategESTOEnhancePerformance.1)selectonlynlynlyndorycolumnStoredatedataTransfer.2)useindexingtospeedupdataretrieval.3)revenmecrycachingtostoreres sultsoffrequent queries.4)

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境