In in this yii tutorial, I show you how to make a login form with dynamic data get from database.
To do this tutorial, simple we just customize the available file CUserIdentity in component folder. By default when creating yii application, It automatically generate for us a login function with account demo/demo or admin/admin . We user again this function and modify a little bit for us. Thing we should do:

  • Create user database.
  • Make model class.
  • Modify UserIdentity file.
  • Modify login action.

– Create your own database or simple run the code below

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `status` smallint(5) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
INSERT INTO user VALUES ('7', 'admin', '[email protected]', 'admin', null);

– Using gii tool to create User model. Don’t know how to use the gii tool, you can go here. Or you can download source code below this article.
– Modify UserIdentity class in yii components .

class UserIdentity extends CUserIdentity
{
 public function authenticate()
 {
  $record = User::model()->findByAttributes(array("email"=>$this->username));
  if($record === null)
  {
   $this->errorCode=self::ERROR_USERNAME_INVALID;
  }
  elseif ($record->password !== $this->password)
  {
   $this->errorCode=self::ERROR_PASSWORD_INVALID;
  }
  else
  {
   $this->setState('userId',$record->id);
   $this->setState('name', $record->email);
   $this->errorCode=self::ERROR_NONE;
  }
  return !$this->errorCode;
 }
}

The ideas of function:

  • We get the email login from login form, check with findByAttributes function in line 5 to get all account which matched with email login.
  • If the returning record is empty, it’ll send error username message.
  • Then if we get an account with existed email login, we’ll compare the current password with old password.
  • All thing is matching, we set the id and email account to session

Change the name of model (in here is email) and attributes (email, password, id) which are match with your yii application’s model.
– LoginForm is created when you create your first yii application. (Don’t know how, click ) .
– Modify actionLogin

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'];
  if($model->validate() && $model->login())
   $this->redirect(Yii::app()->user->returnUrl);
 }
 $this->render('login',array('model'=>$model));
}

I’m just kidding. This is the default code of action login. Nothing need to modify.

In view file, we can use the default login form of yii application example. But remember to change your attributes form match with your model attributes.
Download source code
Good luck with that, if you have any troubles, contact me :)