Home  >  Article  >  Backend Development  >  yii implements creation of verification code instance analysis, yii verification code instance analysis_PHP tutorial

yii implements creation of verification code instance analysis, yii verification code instance analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:21:46824browse

yii implements creation of verification code instance analysis, yii verification code instance analysis

This article describes the method of creating verification code in Yii in the form of examples. The specific steps are as follows:

1. Add the following code under SiteController action():

return array(
 // captcha action renders the CAPTCHA image displayed on the contact page
 'captcha'=>array(
 'class'=>'CCaptchaAction',
 'backColor'=>0xFFFFFF,
 ),
 // page action renders "static" pages stored under 'protected/views/site/pages'
 // They can be accessed via: index.php?r=site/page&view=FileName
 'page'=>array(
 'class'=>'CViewAction',
 ),
);

2. (1) Add code under LoginForm model rules():

//captche class needed
array('verifyCode', 'captcha','allowEmpty'=>!CCaptcha::checkRequirements()),

(2) Add attributes under LoginForm model:

public $verifyCode;

3. Add code under ContactForm model rules():

// verifyCode needs to be entered correctly
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),

4. Add code under login view:

<div class="row">
<&#63;php
echo $form->labelEx($model,'verifyCode');
&#63;>
<&#63;php
$this->widget('CCaptcha');
&#63;>
<&#63;php
echo $form->textField($model,'verifyCode');
&#63;>
<&#63;php
echo $form->error($model,'verifyCode');
&#63;>
</div>

This example code is only a brief description of the main functions. Readers can further improve the program code according to their own project needs to make its functions more practical.

What’s the problem with the verification code in yii not coming out?

This article describes the implementation of Yii verification code. It is just a small example of the author's application. It is also available on the Internet. To summarize, I hope to help Yii enthusiasts in need. 1. The author uses user login, so I add the following code to the sitecontroller: public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF, //Background color
'minLength'=>4, // The shortest is 4 digits
'maxLength'=>4, //The longest is 4 digits
'transparent'=>true, //The display is transparent. When this option is turned off, the background color is displayed
),
);
}
2. Add the following code to the form file (view file such as login.php): 0c77730f95a710e263de4b5e996e889a
44a8922e6607b31d059827e4b04a93b9
3b90ad58d1ab8b2a9a8580f97d4ca40flabelEx($model,'verifyCode'); ?> 076402276aae5dbec7f672f8f4e5cc81
44a8922e6607b31d059827e4b04a93b9
< ; ?php $this->widget('CCaptcha'); ?>
3b90ad58d1ab8b2a9a8580f97d4ca40ftextField($model,'verifyCode'); ?>
ed6b828a1751958f134a511fe41cd994
44a8922e6607b31d059827e4b04a93b9Enter verification code
076402276aae5dbec7f672f8f4e5cc8116b28748ea4df4d9c2150843fecfba68
3b90ad58d1ab8b2a9a8580f97d4ca40ferror($model,'verifyCode'); ? >
950c0e09a208ea6a6c67c615a801fbc6
3. Add the following code to the Loginform model (LoginForm.php), mainly to add attribute fields, otherwise an error will be reported (non-existent attribute) public $username;
public $password;
public $verifyCode;
public $rememberMe;
private $_identity; Through the above operation, we can actually see the verification code, but during the operation we will find that we do not enter it The verification code is still available because we have not specified that verification is required. Add array('verifyCode','required') to LoginForm.php to specify that it is necessary. If we miss the verification code, it will be as shown in the figure below. Display:
This article describes the implementation of Yii verification code. It is just a small example of the author's application. It is also available on the Internet. To summarize, I hope to help Yii enthusiasts in need. 1. The author uses user login, so add the following code to the sitecontroller... The rest of the full text >>

How to use yii’s own verification code?

There are three steps in total. Add one line of code to each layer of controllers, models, and views to achieve it.
The first step is to add
public function actions() {
return array( 'captcha' = >
array(
'class' => 'CCaptchaAction',
'backColor' => 0xF5F5F5,
'transparent'=>true,
'minLength'=> ;4, //The shortest is 4 digits
'maxLength'=>8, //The length is 4 digits
),
);
}
The second step is to add in models As follows:
bc7e8dc4447d08177e46026ec921f5e1beginWidget('CActiveForm')? >
26a80da47b56f4831cdf7ae7bb070476widget('CCaptcha');?>
1474dfd8d57635c571d65b829fea6344textField($model,'verifyCode'); ?>
1474dfd8d57635c571d65b829fea6344error($model,'verifyCode'); ?>
26a80da47b56f4831cdf7ae7bb070476endWidget(); ?>
?> ;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/854351.htmlTechArticleyii implementation of creating verification code example analysis, yii verification code example analysis This article tells the creation of verification code in yii in the form of examples The method, the specific steps are as follows: 1. In SiteController acti...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn