控制器:
//登录
public function actionLogin()
{
$this->layout="//layouts/column3";
$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'];
$model->password = md5($_POST['LoginForm']['password']);
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login()) {
//var_dump(yii::app()->user->getState('userName'));exit();
if((yii::app()->user->getState('isAdmin'))==true) {
$this->redirect(array('meeting/index'));
}
$this->redirect(Yii::app()->user->returnUrl);
}
}
// display the login form
$this->render('login',array('model'=>$model));
}
//注册
public function actionCreate()
{
$this->layout = '//layouts/column3';
$model=new Person;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Person']))
{
$model->attributes=$_POST['Person'];
$model->password = md5($_POST['Person']['password']);
$model->repeat_password = md5($_POST['Person']['repeat_password']);
$model->state = 1;
if($model->save())
$this->redirect(array('login'));
}
$this->render('register',array(
'model'=>$model,
));
}
注册的时候,只需要将 username 字段的 验证规则设置为 unique 即可。意思就是说要此字段的值唯一
请用markdown重新编辑