Login YII menggunakan database MySQL

  • Buka file UserIdentity.php di \protected\components\UserIdentity.php
  • Cari syntax ini, beri tanda comment di syntax nya dengan /* */ menjadi seperti ini
/*
public function authenticate()

{
$users=array(
// username => password
'demo'=>'demo',
'admin'=>'admin',
);
if(!isset($users[$this->username]))
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($users[$this->username]!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;
}
}
*/
  • Lalu tambahkan syntax ini di bawahnya
public function authenticate()
    {
        $user=User::model()->findByAttributes(array('username'=>$this->username));
        if($user===null){
            $this->errorCode=self::ERROR_USERNAME_INVALID;  
}
        else if($user->password!==$this->password)
            {$this->errorCode=self::ERROR_PASSWORD_INVALID;}
        else
        {
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }
  • Simpan file
Catatan :
Nama tabel di database harus user, dan nama kolomnya iduser/userid ; username ; password