I making a login session with one condition if User is not found in Table A then start looking in Table B, here's the code:

```
$EMP_NO = strtoupper($this->input->post('empcd'));
        $passwd = strtoupper($this->input->post('password'));

        $getUser = $this->db->query("SELECT EMP_NO, EN_NAME, UPPER(cryptit.decrypt(PASSWORD,EMP_NO)) PASSWORD2, MAILADDRESS FROM tkgw_user_information WHERE EMP_NO = '$EMP_NO'");

        if ($getUser->num_rows() == 1) {
            $user = $getUser->row();

            if ($passwd == $user->PASSWORD2) {

                $session_data = [
                    'iduser' => $user->EMP_NO,
                    'nama' => $user->EN_NAME,
                    'empcd' => $user->EMP_NO,
                    'email' => $user->MAILADDRESS,
                    'loginuser' => TRUE,
                ];

                $this->session->set_userdata($session_data);
                $this->session->set_flashdata('success', 'Welcome! ' . $user->EN_NAME);
                redirect(base_url('dashboard'));
            } else {
                $this->session->set_flashdata('error', 'Wrong Password!');
                redirect(base_url('sign-in'));  
            }

            } else {
                $findUser = $this->db->query("SELECT UPPER(EMP_NO), EN_NAME, UPPER(PASSWORD) PASSWORD2, MAILADDRESS FROM ITSJ_USER_ACCOUNT WHERE EMP_NO = '$EMP_NO'");
                if ($findUser->num_rows() == 1) {
                    $userx = $findUser->row();

                    if ($passwd == $userx->PASSWORD2) {

                        $session_data = [
                            'iduser' => $userx->EMP_NO,
                            'nama' => $userx->EN_NAME,
                            'empcd' => $userx->EMP_NO,
                            'email' => $userx->MAILADDRESS,
                            'loginuser' => TRUE,
                        ];
                        $this->session->set_userdata($session_data);
                        $this->session->set_flashdata('success', 'Welcome! ' . $userx->EN_NAME);
                        redirect(base_url('dashboard'));

                    } else {
                        $this->session->set_flashdata('error', 'Wrong Password!');
                        redirect(base_url('sign-in'));  
                    }
                }else{
                        $this->session->set_flashdata('error', 'User not avalable!');
                        redirect(base_url('sign-in'));
                }
            }
        }

```

With those code I able to login via Table B when the User is not exist in Table A, but every time I'm switching page it keeps asking me to login again, seems like the session is over or destroyed.
But if I login via Table A (the first condition), the session remain intact.

Can you please help me finding the solution, thank you.

Edit:

checking if user logged or not on each page:

```
if ($this->session->userdata('iduser') == NULL) {
    $this->session->set_flashdata('Warning', 'Please Login First');
    redirect(base_url('sign-in'));
}

```