Search code examples
phpcodeignitersessioncodeigniter-3codeigniter-session

My session doesnot work properly


I have the problem, that the following if condition always goes into the first if condition, never into the elseif condition.

This is my code below :

if ($this->session->has_userdata('role') == 1) {
    $this->session->set_flashdata('success', 'Welcome ' . $userInfo[0]->name . '!');
    redirect('adashboard');    
} elseif ($this->session->has_userdata('role') == 2) {
    $this->session->set_flashdata('success', 'Welcome ' . $userInfo[0]->name . '!');
    redirect('udashboard');
}

Can you tell my why?


Solution

  • Hope this will help you :

    Use userdata instead of has_userdata : userdata returns Value of the specified item key

    if ($this->session->userdata('role') == 1) 
    {
        $this->session->set_flashdata('success', 'Welcome ' . $userInfo[0]->name . '!');
        redirect('adashboard');    
    } 
    elseif ($this->session->userdata('role') == 2) 
    {
        $this->session->set_flashdata('success', 'Welcome ' . $userInfo[0]->name . '!');
        redirect('udashboard');
    }
    

    For more : https://www.codeigniter.com/user_guide/libraries/sessions.html