Hello, I have create Register page and Login systems with Yii framework, but it is not working. There is no php or yii error, but it seems that registered users can’t sign in.This works, as the user was created in the table and I could not login / logout.
But what I want to ask is it really so simple or am I doing something wrong?
SiteController.php
public function actionRegister()
{
$model=new registration;
// 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['registration']))
{
$model->attributes=$_POST['registration'];
if ($model->validate())
{
$newUser->username = $model->username;
$newUser->password = $model->password;
$newUser->email = $model->email;
$newUser->firstname = $model->firstname;
$newUser->lastname = $model->lastname;
$newUser->last_modified = date('Y-m-d');
if($model->save())
{
$identity=new UserIdentity($newUser->username,$model->password);
$identity->authenticate();
Yii::app()->user->login($identity,0);
//redirect the user to page he/she came from
$this->redirect(Yii::app()->user->returnUrl);
$this->refresh();
}
}
}
$this->render('register',array('model'=>$model));
}
protected/view/site/register.php
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.dynDateTime.js"></script>
<script type="text/javascript" src="js/calendar-en.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#dateDefault").dynDateTime(); //defaults
});
</script>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/calendar-win2k-cold-1.css" />
<h1 style="color:#FFFFFF">Register</h1>
<p style="color:#FFFFFF">Please fill out the following form with your register credentials:</p>
<div class="form" style="color:#FFFFFF">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableAjaxValidation'=>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'); ?>
<div class="clear"></div>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<div class="clear"></div>
<?php echo $form->error($model,'password'); ?>
</div>
<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,'firstname'); ?>
<?php echo $form->textField($model,'firstname'); ?>
<?php echo $form->error($model,'firstname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lastname'); ?>
<?php echo $form->textField($model,'lastname'); ?>
<?php echo $form->error($model,'lastname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'birthdate'); ?>
<?php echo $form->textField($model,'birthdate',array('id'=>'dateDefault')); ?>
<?php echo $form->error($model,'birthdate'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Register'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
model/registration.php
<?php
/**
* This is the model class for table "registration".
*
* The followings are the available columns in table 'registration':
* @property integer $id
* @property string $username
* @property string $password
* @property string $email
* @property string $firstname
* @property string $lastname
* @property string $birthdate
*/
class registration extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return registration the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'registration';
}
/**
* @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('username, password, email, firstname, lastname, birthdate', 'required'),
array('username, password, email, firstname, lastname', 'length', 'max'=>100),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, username, password, email, firstname, lastname, birthdate', '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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'username' => 'Username',
'password' => 'Password',
'email' => 'Email',
'firstname' => 'Firstname',
'lastname' => 'Lastname',
'birthdate' => 'Birthdate',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('username',$this->username,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('firstname',$this->firstname,true);
$criteria->compare('lastname',$this->lastname,true);
$criteria->compare('birthdate',$this->birthdate,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
Plz Help Me
Thnks