Search code examples
phpcodeignitersessioncodeigniter-4

how to set different sessions for user and admin | CodeIgniter


hello i am working on a project where user and admin have different logins I want to set different sessions for user and admin here is I how set admin session

$this->session->set('adminData', [
            'id'            => $teacher['id'],
            'name'          => $teacher['name'],
            'email'         => $teacher['email'],
            'new_email'     => $teacher['new_email']
        ]);

user's session data as follows

$this->session->set('userData', [
                'id'            => $teacher['id'],
                'name'          => $teacher['name'],
                'email'         => $teacher['email'],
                'new_email'     => $teacher['new_email']
            ]);

but when validating user

if ($this->session->isLoggedIn) {
            return redirect()->to('admin');
        }

the only problem I am facing that either I can login as admin or user not both when I try to login as user when I am already logged as admin, it redirects me to user/account.

Is there any method that i can set isAdminLoggedIn for admin and can use isLoggedIn for user?


Solution

  • you can set isAdminLoggedIn before or after adminData.

    $this->session->set('isAdminLoggedIn', true);
    

    and can destroy the session when you logged out

    $this->session->remove(['isAdminLoggedIn']);
    

    or together with

    $this->session->remove(['isAdminLoggedIn', 'adminData']);