zuboje
zuboje

Reputation: 726

Codeigniter 3 and session

I had sessions working fine in 2.x version of CI. I used to save them using default CI session option (browser I believe), but I've decided to use database now. I've setup table and everything else in config file. Problem is I never get any errors. Session and encryption library are in autoload config file. In my model I use following code to put data into session:

$this->session->set_userdata(array(
  'email' => $email,
  'is_logged_in' => 1)
); 

But when I try to use:

var_dump($this->session->userdata());

I get following output:

array(1) { ["__ci_last_regenerate"]=> int(1448664996) }

nothing is added to session, no errors showing up. Can anyone help?

EDIT:

config.php

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'mydomain';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

autoload.php

$autoload['libraries'] = array('database', 'email', 'encryption', 'session');

I've followed all instructions documentation is showing.

EDIT 2:

controller:

public function login_into_site()
    {
        if($this->input->post('viaAjax') == 1)
        {
            $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|callback_validate_credentials');
            $this->form_validation->set_rules('password', 'Password', 'required|md5|trim');
            $user_url = base64_decode($this->input->post('rand'));

            if ($this->form_validation->run())
            {
                $sulo = array();
                $sulo['res'] = '1';
                $sulo['address'] = $user_url;
                echo json_encode($sulo);
            }
            else
            {
                $formError = array();
                $formError['email'] = form_error('email');
                $formError['password'] = form_error('password');
                echo json_encode($formError);
            }
        }
        else
        {
            echo json_encode("fail");
        }
    }

    public function validate_credentials()
    {
        $this->load->model('user/model_user_auth');
        $this->model_user_auth->can_log_in($message);
        if($message == 1)
        {
            return true;
        }
        else if($message == 2)
        {
            $this->form_validation->set_message('validate_credentials','not activated');
            return false;
        }
        else
        {
            $this->form_validation->set_message('validate_credentials','bad username or password');
            return false;
        }
    }

model:

public function can_log_in(&$message)
    {
        $email = $this->input->post('email');
        $this->db->where('email',$email);
        $this->db->where('password',md5($this->input->post('password')));
        $query = $this->db->get('users');
        $message = '';
        if($query->num_rows() == 1)
        {
            $this->session->set_userdata(array(
                'email' => $email,
                'is_logged_in' => 1)
            );

            foreach ($query->result() as $row)
            {
                $activated = $row->activate;
                $membership = $row->membership;
            }
            if($activated == NULL && $membership == 'a')
            {
                $message = 1; //success
            }
            else if($activated != NULL && $membership == 'a')
            {
                $message = 2; //not activated
            }
            else
            {
                $message = 3; // user exist but info wrong
            }
        }
        else
        {
            $message = 0; // user does not exist
        }
    }

Model generates $message = 1; because I get return as "TRUE" in controller callback function and controller function that loges user in proceed without any errors

Upvotes: 1

Views: 1963

Answers (1)

zuboje
zuboje

Reputation: 726

Solved. Problem was that my cookie was overriding database session. In config file if using database for session, this line cannot have value"

$config['sess_cookie_name'] = 'mydomain';

In order for session to work using database, that value has to be empty:

$config['sess_cookie_name'] = '';

If it is not empty, CI will default to cookies for your session. This does not mean you cannot use cookies. You just have to loaded them via helper and they will work just fine.

Upvotes: 2

Related Questions