Php Yii登录不工作
我是Yii PHP框架的新手,我正在尝试使用登录表单。我知道当你安装testdrive应用程序时,Yii上已经有了登录功能。我只是编辑它,通过数据库登录,它不工作。我没有任何错误的代码,但当我登录时,它总是说不正确的密码或用户名。这是我的密码 对于UserIdentity.phpPhp Yii登录不工作,php,login,yii,Php,Login,Yii,我是Yii PHP框架的新手,我正在尝试使用登录表单。我知道当你安装testdrive应用程序时,Yii上已经有了登录功能。我只是编辑它,通过数据库登录,它不工作。我没有任何错误的代码,但当我登录时,它总是说不正确的密码或用户名。这是我的密码 对于UserIdentity.php class UserIdentity extends CUserIdentity { /** * Authenticates a user. * The example implementation makes s
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 text plain中,请不要使用crypt
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!==$this->password) // changed
$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;
}
}
在您的编码中,行
else if($record->password!==crypt($this->password,$record->password))
正在将密码与字符串哈希进行比较(crypt()
生成字符串哈希/加密)
如果在数据库中保存密码而不加密,则可以直接比较用户输入密码和在数据库中保存的密码,无需应用crypt()
现在将代码更改为
public function authenticate()
{
$record = User::model()->findByAttributes(array('username' => $this->username));
if ($record === null)
$this->errorCode = self::ERROR_USERNAME_INVALID;
else if (trim($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;
}
您是如何将密码保存在db中的?你加密了吗?没有。密码是纯文本的。我只是执行了mysql插入命令。我做错了吗?我试过了,但出了差错。属性“User.title”未定义。确定这意味着登录查询运行良好,现在我将编辑标题问题的答案。请确保用户表中有标题列,否则设置public$title;到用户模型类的顶部
public function authenticate()
{
$record = User::model()->findByAttributes(array('username' => $this->username));
if ($record === null)
$this->errorCode = self::ERROR_USERNAME_INVALID;
else if (trim($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;
}