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

WBOY
Release: 2016-07-13 10:21:46
Original
827 people have browsed it

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',
 ),
);
Copy after login

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

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

(2) Add attributes under LoginForm model:

public $verifyCode;
Copy after login

3. Add code under ContactForm model rules():

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

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>
Copy after login

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):
< div>
< ?php echo $form->labelEx($model,'verifyCode'); ?>

< div>
< ; ?php $this->widget('CCaptcha'); ?>
< ?php echo $form->textField($model,'verifyCode'); ?>
< / div>
< div>Enter verification code


< ?php echo $form->error($model,'verifyCode'); ? >
< /div>
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:
public $verifyCode; //You must first define
public function rules()
{
return array(
array('verifyCode', 'captcha '),
);
}
The third step is to add the following in views (use small objects here)
beginWidget('CActiveForm')? >
widget('CCaptcha');?>
textField($model,'verifyCode'); ?>
error($model,'verifyCode'); ?>
endWidget(); ?>
?> ;

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...
Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template