this is view login.php
<?php
/* @var $this SiteController */
/* @var $model LoginForm */
/* @var $form CActiveForm */
$this->pageTitle=Yii::app()->name . ' - Login';
?>
<h1>Login</h1>
<p>Please fill out the following form with your login credentials:</p>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'enableAjaxValidation'=>true,
),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email'); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Login'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
controller LoginController
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class LoginController extends CController
{
public function actionIndex()
{
$model = new User;
// if it is ajax validation request
// collect user input data
if(isset($_POST['user']))
{
$model->attributes=$_POST['user'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
$this->redirect(array('/site/index'));
}
// display the login form
$this->render('/student/login',array('model'=>$model));
}
}
?>
Model is student
<?php
/**
* This is the model class for table "student".
*
* The followings are the available columns in table 'student':
* @property integer $id
* @property integer $user_id
* @property string $first_name
* @property string $last_name
* @property string $gender
* @property string $dob
* @property string $mobile_no
* @property string $address
* @property string $country
*
* The followings are the available model relations:
* @property User $user
*/
class Student extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'student';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('user_id, first_name, last_name, gender, dob, mobile_no, address, country', 'required'),
array('user_id', 'numerical', 'integerOnly'=>true),
array('first_name, last_name', 'length', 'max'=>50),
array('gender', 'length', 'max'=>1),
array('mobile_no', 'length', 'max'=>15),
array('address', 'length', 'max'=>200),
array('password', 'authenticate'),
array('email', 'authenticate'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, user_id, first_name, last_name, gender, dob, mobile_no, address, country', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
//'id' => 'ID',
'user_id' => 'User id',
'first_name' => 'First Name',
'last_name' => 'Last Name',
'gender' => 'Gender',
'dob' => 'Dob',
'mobile_no' => 'Mobile No',
'address' => 'Address',
'country' => 'Country',
);
}
/**
* 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->email,$this->password);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect email or password.');
}
}
/**
* 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->email,$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;
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('user_id',$this->user_id);
$criteria->compare('first_name',$this->first_name,true);
$criteria->compare('last_name',$this->last_name,true);
$criteria->compare('gender',$this->gender,true);
$criteria->compare('dob',$this->dob,true);
$criteria->compare('mobile_no',$this->mobile_no,true);
$criteria->compare('address',$this->address,true);
$criteria->compare('country',$this->country,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Student the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
in component i create UserIdentity.php
<?php
/**
* UserIdentity represents the data needed to identity a user.
* It contains the authentication method that checks if the provided
* data can identity the user.
*/
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.
*/
public function authenticate()
{
$record= User::model()->findByAttributes(array('email'=>$this->email));
if($record===null)
$this->errorCode= self::ERROR_EMAIL_INVALID;
elseif ($record->password!==$this->password)
$this->errorCode= self::ERROR_PASSWORD_INVALID;
else {
$this->errorCode= self::ERROR_NONE;
}
return !$this->errorCode;
}
}
Is it correct .because login is actually not working .
plz help me