[英]Normal php login to YII login
我有一個正常的php登錄名,該登錄名連接到數據庫對用戶進行身份驗證,現在我需要將其轉換為使用yii框架的登錄名,任何人都可以告訴我這樣做..我應該做的第一件事是我可以將其轉換為yii登錄。 以下是我必須調用的當前登錄功能
function login($usr,$pwd) {
$query = "SELECT * FROM login WHERE us.username='$usr' AND us.password='$pwd'; ";
$dataReader=$command->query();
$row = mysql_fetch_array($dataReader);
$log = new stdClass();
if($row) {
$pro->accountID = (int)$row['accountID'];
$pro->accountname = $row['accountname'];
$pro->usertype = (int)$row['usertype'];
$string = rand() . 'SURVAYLAND' . rand() . $usr. $pwd;
$_SESSION['SURVEY_AUTHENTICATE_KEY'] = md5($string);
} else {
$pro = false;
}
}
每當調用Yii::app()->user
,都會獲得CWebUser
的實例。 這是Yii代表用戶當前正在查看您的應用程序的方式。
該用戶可以登錄或訪問應用程序而無需登錄(即成為訪客)。
CWebUser類具有一個名為login
的方法,如您所料,它可以登錄用戶。
方法login()
以實現IUserIdentity接口的對象作為參數。
制作自己的最簡單的方法是創建一個簡單的類(例如,將其稱為MyIdentity):
//this class' constructor takes a username and a password
class MyIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
// check username and password in DB
// return true or false to signal whether user should be logged in
}
public function getId()
{
return $this->_id;
}
}
然后使用您剛創建的內容實際登錄用戶:
// Login a user with the provided username and password.
$identity=new MyIdentity($username,$password);
if($identity->authenticate())
Yii::app()->user->login($identity);
else
echo $identity->errorMessage;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.