//This is form file
<?php
$this->pageTitle=Yii::app()->name . ’ - SchoolLogin’;
$this->breadcrumbs=array(
'Login',
);
?>
<?php
$this->menu=array(
array('label'=>'Sign up', 'url'=>array('schoolregister')),
);
?>
<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,
),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
</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>
//model file
<?php
abstract class login extends CActiveRecord
{
public $username;
public $password;
private $_identity;
public function tableName()
{
return 'tbl_login';
}
public function rules()
{
return array(
array('username, password,email,contact_no', 'required'),
array('username, password', 'length', 'max'=>255),
array('username','match' ,'pattern'=>'/^[A-Za-z_]+$/u','message'=>'Enter only letters!'),
array('email, password', 'length', 'max'=>255),
array('id, name, email, password', 'safe', 'on'=>'search'),
array('contact_no', 'length', 'max'=>10),
array('contact_no','numerical', 'integerOnly'=>true),
array('username', 'length', 'min'=>3, 'max'=>12),
array('email', 'email', 'message'=>'Please insert valid Email'),
array('email', 'email','message'=>"The email isn't correct"),
array('email', 'unique','message'=>'Email already exists!'),
array('password', 'authenticate'),
);
}
public function relations()
{
return array(
);
}
public function attributeLabels()
{
return array(
'id'=>'ID',
'username'=>'Username',
'password'=>'Password',
'email'=>'Email',
'contact_no'=>'Contact NO',
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('username',$this->username,true);
$criteria->compare('password',$this->password,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
}
/**
* 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();
return true;
}
else
return false;
}
}
//controller
public function actionSchoollogin()
{
$model=new Login;
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
if(isset($_POST['Login']))
{
$model->attributes=$_POST['Login'];
if($model->validate() && $model->login())
$this->redirect(array('schoolform'));
}
$this->render('schoollogin',array('model'=>$model));
}
//useridentity
<?php
class UserIdentity extends CUserIdentity
{
public function authenticate()
{
$user = Login::model()->findByAttributes(array('username'=>$this->username));
if ($user===null)
{
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
else if
($user->password !== ($this->password) )
{
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
else
{
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
}