Home > Backend Development > PHP Tutorial > How to use Captcha verification code in Yii, yiicaptcha verification code_PHP tutorial

How to use Captcha verification code in Yii, yiicaptcha verification code_PHP tutorial

WBOY
Release: 2016-07-12 09:02:18
Original
982 people have browsed it

How Yii uses Captcha verification code, yiicaptcha verification code

The example in this article describes how Yii uses Captcha verification code. Share it with everyone for your reference, the details are as follows:

For detailed code, please refer to: Yii’s own sample code post project, which contains a contact form that uses a verification code.

1. Model:

Add the verification code to an attribute of UserLogin:

class UserLogin extends CFormModel
{
 public $username;
 public $password;
 public $rememberMe;
 public $verifyCode;
 public function rules()
 {
  return array(
   // username and password are required
   array('username, password,verifyCode', 'required'),
   // rememberMe needs to be a boolean
   array('rememberMe', 'boolean'),
   // password needs to be authenticated
   array('password', 'authenticate'),
   // verifyCode needs to be entered correctly
   array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
  );
 }
 /**
  * Declares attribute labels.
  */
 public function attributeLabels()
 {
  return array(
   'rememberMe'=>Yii::t('user',"Remember me next time"),
   'username'=>Yii::t('user',"username or email"),
   'password'=>Yii::t('user',"password"),
   'verifyCode'=>Yii::t('user','Verification Code'),
  );
 }
}

Copy after login

2. Controller

Add mapping action CCaptchaAction to the LoginController controller

public function actions()
{
 return array(
  // captcha action renders the CAPTCHA image displayed on the contact page
  'captcha'=>array(
   'class'=>'CCaptchaAction',
   'backColor'=>0xf4f4f4,
   'padding'=>0,
   'height'=>30,
   'maxLength'=>4,
  ),
  );
}
ublic function actionLogin()
{
 if (Yii::app()->user->isGuest) {
  $model=new UserLogin;
  // collect user input data
  if(isset($_POST['UserLogin']))
  {
   $model->attributes=$_POST['UserLogin'];
//在此核对验证码
   if($this->createAction('captcha')->validate($model->verifyCode, false))
   {
    // validate user input and redirect to previous page if valid
    if($model->validate()) {
    //admin login only
    if( Yii::app()->getModule('user')->isAdmin()==1 )
    {
    $this->lastViset();
    if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false)
     $this->redirect(Yii::app()->controller->module->returnUrl);
    else
     $this->redirect(Yii::app()->user->returnUrl);
    }else
    {//if no admin when login out
     $this->redirect(Yii::app()->controller->module->logoutUrl);
    }
   }
   }else
   {//提示错误
    $model->addError('verifyCode','验证码不对');
   }
  }
  // display the login form
  $this->render('/user/login',array('model'=>$model));
 } else
  $this->redirect(Yii::app()->controller->module->returnUrl);
}

Copy after login

Before verifying the username and password, check the verification code:

if($this->createAction('captcha')->validate($model->verifyCode, false))
{

Copy after login

3. view

Show the verification code image in the view and input box

<&#63;php $this->widget('CCaptcha'); &#63;>
  <&#63;php echo CHtml::activeTextField($model,'verifyCode',array('tabindex'=>1)); &#63;>
<img src="http://www.XXXX.net/uploads/123456.jpg" alt="">

Copy after login

I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

Articles you may be interested in:

  • yii user registration form verification example
  • Comprehensive form verification rules of PHP Yii framework
  • Yii framework form form usage Example
  • Yii's form generator usage example that does not rely on Model
  • Yii framework form model usage and submitting form data in the form of array example
  • Implementing front and backend login processing in Yii New method
  • yii method to remove asterisks in required fields

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1085886.htmlTechArticleYii's method of using Captcha verification code, yiicaptcha verification code This article describes how Yii uses Captcha verification code. Share it with everyone for your reference, the details are as follows: Detailed code can be found...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template