[center]Help me![/center]
[center]I not login with error Incorrect username or password.[/center]
1. File UserIdentity.php
<?php
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record=User::model()->findByAttributes(array('username'=>$this->username));
if($record===null)
{
$this->_id='user Null';
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
else if($record->password!==$this->password) // here I compare db password with passwod field
{ $this->_id=$this->username;
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}else{
$this->_id=$record['username'];
$this->setState('email', $record['email']);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId() // override Id
{
return $this->_id;
}
}
?>
2.File LoginForm.php in model
<?php
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
private $_identity;
/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
*/
public function rules()
{
return array(
// username and password are required
array('username, password', 'required'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'rememberMe'=>'Remember me next time',
);
}
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->username,$this->password);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect username or password.');
}
//var_dump($this->_identity); die('aaa');
}
/**
* Logs in the user using the given username and password in the model.
* @return boolean whether login is successful
*/
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}
}
3.actionLogin in controller
public function actionLogin()
{
$model=new LoginForm;
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
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(array("index"));
}
}
$this->render('login',array('model'=>$model));
}
4. View login.php
<?php
$this->pageTitle=Yii::app()->name . ’ - Login’;
$this->breadcrumbs=array(
'Login',
);
?>
<div class=‘content_right’>
<div class="content_detail">
<div class="form">
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<table width="700" height="500" align="center" border="1">
<tr>
<td align="center">
<h2 class="ac">Login User</h2>
<?php echo $form->error($model,'username'); ?>
<p style="margin-bottom: 30px">
Username : <input type="text" id="LoginForm_username" class="full" value="" name="LoginForm[username]" required="required" placeholder="Username" />
</p>
<?php echo $form->error($model,'password'); ?>
<p style="margin-bottom: 30px">
Password : <input type="password" id="LoginForm_password" class="full" value="" name="LoginForm[password]" required="required" placeholder="Password" />
</p>
<p class="clearfix">
<span class="fl" style="line-height: 23px;">
<label class="choice" for="remember">
<input type="checkbox" id="remember" class="" value="1" name="LoginForm[rememberMe]"/>
Remember me
</label>
</span>
<br/>
<button class="fr" type="submit">Đăng nhập</button>
</p>
</td>
</tr>
</table>
<?php $this->endWidget(); ?>
</div><!-- form -->
</div>
</div>
Help me!