Niyaz Kannanchery
Niyaz Kannanchery

Reputation: 1

Setting Session error in CodeIgniter

I m trying to set Session in codeigniter. when a person try to login with email id and password.

1st) using email id find his/her role. then return that result in session and redirect to respective link. the code I typed below. Could you please solve this issue?

Controller

public function login_validation() {
        $this -> load -> library('form_validation');
        $this -> form_validation -> set_rules('email', 'Email', 'required|trim');
        $this -> form_validation -> set_rules('password', 'Password', 'required ');

        if ($this -> form_validation -> run()) {

            $this -> load -> model('model_database');
            $sessiondata['var_userrole'] = $this -> model_database -> login_session();

            $this -> session -> set_userdata($sessiondata);
            if ($var_userrole == admin) {

                redirect('welcome/admin');

            } else if ($var_userrole == staff) {

                redirect('welcome/staff');

            } else if ($var_userrole == student) {

                redirect('welcome/student');

            } else {

                redirect('welcome/login');

            }

Model

public function login_session() {
    $email = $this -> input -> post('email');
    $login_id = $this -> db -> query("SELECT var_userrole FROM tbl_login where var_email = '" . $email . "'");
    return  $login_id -> row_array();
}

Upvotes: 0

Views: 146

Answers (1)

umefarooq
umefarooq

Reputation: 4574

hi first of all $var_userrole is not defined anywhere which you are checking in if statements

Controller

public function login_validation() {
        $this -> load -> library('form_validation');
        $this -> form_validation -> set_rules('email', 'Email', 'required|trim');
        $this -> form_validation -> set_rules('password', 'Password', 'required ');

        if ($this -> form_validation -> run()) {

            $this -> load -> model('model_database');
            $var_userrole = $this -> model_database -> login_session();// was undefined 
            $sessiondata['var_userrole'] = $var_userrole

            $this -> session -> set_userdata($sessiondata);
            if ($var_userrole == admin) {

                redirect('welcome/admin');

            } else if ($var_userrole == staff) {

                redirect('welcome/staff');

            } else if ($var_userrole == student) {

                redirect('welcome/student');

            } else {

                redirect('welcome/login');

            }

Model

public function login_session() {
    $email = $this -> input -> post('email');
    $login_id = $this -> db -> query("SELECT var_userrole FROM tbl_login where var_email = '" . $email . "'");
      //$row =  $login_id -> row() // as object return $row->var_userrole;
      $row =  $login_id -> row_array();
    return $row['var_userrole']
}

well if you are developing something new for auth before that check some auth system in CI like ion_auth has group which you can mange and check user from which group its belong Ion auth

Upvotes: 0

Related Questions