读书人

以料登进

发布时间: 2013-11-09 17:06:47 作者: rapoo

以料登入

以料登入

由於 Yii 定的原始框架程式, 用定在UserIdentity.php 的 demo admin 密:

??? public function authenticate()
??? {
??? ??? $users=array(
??? ??? ??? // username => password
??? ??? ??? 'demo'=>'demo',
??? ??? ??? 'admin'=>'admin',
??? ??? );

而一般的程式都希望能利用特定的 User 料表中的密行登入, 因此必行如下修改:

component\UserIdentity.php 中的

??? public function authenticate()
??? {
? // 根使用者所入的 $this->username 在 User model 中(即料表 user), 搜的
? // 用名
??? ??? $user=User::model()->find('LOWER(username)=?',array(strtolower($this->username)));
??? // 假如查此, 回覆
??? ??? if($user===null)
??? ??? ??? $this->errorCode=self::ERROR_USERNAME_INVALID;
??? // 若密不符, 也是回覆
??? ??? else if(!$user->validatePassword($this->password))
??? ??? ??? $this->errorCode=self::ERROR_PASSWORD_INVALID;
??? ??? else
??? ??? {
??? // 利登入
??? ??? ??? $this->_id=$user->id;
??? ??? ??? $this->username=$user->username;
??? ??? ??? $this->errorCode=self::ERROR_NONE;
??? ??? }
??? ??? return $this->errorCode==self::ERROR_NONE;
??? }

用利用料表 user 使用者登入.?

在 User.php Model 中需要:

/**
?* Generates a salt that can be used to generate a password hash.
?* @return string the salt
?*/
protected function generateSalt()
{
??? return uniqid('',true);
}

截至目前止, 已完成下列事:

  1. Yii 系已正安, framework 目位於 www 目之外.
  2. Blog 骨架程式已建立.
  3. Blog 系所需要的 SQLite 料表已建立.
  4. main.php 中的料定已配合更改.
  5. 料表的 model CRUD 作程式已建立.
  6. 使用者登入的, 已由原先的定密, 改由 tbl_user 料表行.

读书人网 >编程

热点推荐