我是Yii PHP Framework的新手,我試圖在登錄表單上工作。我知道在安裝測試驅動器應用程序時,Yii上已經有了一個登錄功能。我只是編輯它通過數據庫登錄,它不工作。我在代碼中沒有任何錯誤,但是當我登錄時總是說錯誤的密碼或用戶名。這是我的代碼。Yii登錄不起作用
對於UserIdentity.php
class UserIdentity extends CUserIdentity
{
/**
* Authenticates a user.
* The example implementation makes sure if the username and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
* against some persistent user identity storage (e.g. database).
* @return boolean whether authentication succeeds.
*/
private $_id;
public function authenticate()
{
$record=User::model()->findByAttributes(array('username'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==crypt($this->password,$record->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState('title', $record->title);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
下面是SiteController.php
public function actionLogin()
{
$model=new LoginForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
$this->redirect(Yii::app()->user->returnUrl);
}
// display the login form
$this->render('login',array('model'=>$model));
}
你能幫助我嗎?謝謝!
你是如何在db中保存密碼的?你有加密嗎? – Hearaman
編號密碼爲純文本。我只是執行mysql插入命令。我做錯了嗎? – Yassi