Krystian Niko Liris
Krystian Niko Liris

Reputation: 9

Codeigniter sessions php

class MY_Controller extends CI_Controller
{
    public $data = array();

    function __construct()
    {
        parent::__construct();
        if($this->session->userdata('group_id') == FALSE) {
            redirect('user/login');
        }else {
            //How download sessions?
            //How Get the session given group_id?
            //How Redirect to the appropriate controller?
            if grup_id == 1 then goto administrator/dashboard
               //if not exist grup id then goto /index
            }
        }
    }

IN CODEIGNITER FRAMEWORK PHP

Upvotes: 0

Views: 89

Answers (3)

There is a type mismatch. group_id is either an int or null, not a boolean. Also retrieve the value from session once and assign it to a variable in order to avoid calling session's member function over and over.

function __construct()
{
    parent::__construct(); 
    group_id = $this->session->group_id;
    if (is_int(group_id)) // if group_id is set and assigned an int 
    {
        switch ($group_id)
        {
            case 1: 
                redirect('administrator/dashboard');
                break; 
            ...
            default:
                redirect('defaultpage');
        }
    }
    else
    {
        redirect('user/login');    
    }
}

This being said, you may not want to reinvent the wheel and use ion_auth, the user management and authentication class for CI.

Upvotes: 0

Ewomazino Ukah
Ewomazino Ukah

Reputation: 2366

Depending on what you are trying to achieve you can check out ion auth library https://github.com/benedmunds/CodeIgniter-Ion-Auth . Using an already existing library that suits your requirements can drastically improve your development speed for a project.

Upvotes: 0

NikuNj Rathod
NikuNj Rathod

Reputation: 1658

You can try this solution for your problem.

class MY_Controller extends CI_Controller
{
    public $data = array();

    function __construct()
    {
        parent::__construct();

        if($this->session->userdata('group_id') == 1) {
            redirect('administrator/dashboard');
        } esle if($this->session->userdata('group_id') == 2) {
            redirect('manager/dashboard');
        } esle if($this->session->userdata('group_id') == 3) {
            redirect('user/dashboard');
        } else {
            redirect('user/login');
        }
    }
}

Upvotes: 1

Related Questions