php - How to create a login module with two user levels in Yii -


i make login page 2 different user levels, doctor & parent. how can in yii....

two tables listed below....for both users email address username.

create table doctor ( doctor_email varchar(50), password varchar(50), doctor_name varchar(50), doctor_phone int(10), speciality varchar(200), primary key (doctor_email) );

create table parent ( parent_email varchar(50), parent_name varchar(50), password varchar(20), parent_address varchar(100), parent_phone int(10), primary key (parent_email) );

this authenticate() function in useridentity.php

public function authenticate() {

    $record1 = doctor::model()->findbyattributes(array('doctor_email' => $this->username));     $record2 = parents::model()->findbyattributes(array('parent_email' => $this->username));      if ($record1 !== null) {         if ($record1->password == $this->password) {             $this->errorcode = self::error_none;          } else {             $this->errorcode = self::error_password_invalid;         }     } else if ($record2 !== null) {         if ($record2->password == $this->password) {             $this->errorcode = self::error_none;          } else {             $this->errorcode = self::error_password_invalid;         }     }     return !$this->errorcode; } 

this login() function in models/loginform.php

public function login() {

    if ($this->_identity === null) {         $this->_identity = new useridentity($this->username, $this->password);          $this->_identity->authenticate();         if ($this->_identity->record1 !== null)             $this->isdoc = true;         else if ($this->_identity->record2 !== null)             $this->isdoc = false;     }     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; } 

and actionlogin() in sitecontoller.php

public function actionlogin() { $model = new loginform;

    // if 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'];           if ($model->validate() && $model->login()) {               if ($model->isdoc) {                 $this->redirect(array('/doctor/index'));              } elseif ($model->isdoc == false) {                 $this->redirect(array('/parents/index'));              }         }     }     // display login form     $this->render('login', array('model' => $model)); } 

when login both users redirect '/parents/index'. can tell me wrong? or idea.....thanx in advance..

as can see problem $model->isdoc null. , have used $model->isdoc == false

i mean double equal operator. condition getting true.

try $model->isdoc === false (triple equal operator)

i pretty sure take login form again.

if not wrong above real problem persists here

if ($this->_identity->record1 !== null)             $this->isdoc = true;         else if ($this->_identity->record2 !== null)             $this->isdoc = false; 

try var_dump($this->_identity->record1) var_dump($this->_identity->record2) , check whether of them not getting null in case

or can try this

in useridentity class need declare 2 variable s as

class useridentity extends cuseridentity { public $record1; public $record2;  } 

now in authenticate()

change $record1 $this->record1 , $record2 $this->record2


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -