Cian Woods
Cian Woods

Reputation: 23

Creating a session with CodeIgniter (3.1.0)

I'm currently learning to use CodeIgniter for a college project and have been assigned the task by my group to create user login and generate a session for that user. I have the login working and checking credentials in database but I have run into trouble with setting up a session.

Login model that gets user details

 <?php
 Class Login_Model extends CI_Model
 {
 function loginUser($username, $password)
 {
 $this -> db -> select('id, userName, password');
 $this -> db -> from('user_tbl');
 $this -> db -> where('userName', $username);
 $this -> db -> where('password', $password);
 $this -> db -> limit(1);

  $query = $this -> db -> get();

  if($query -> num_rows() == 1)
  {
  return $query->result();
 }
 else
 {
  return false;
}
}
}

Login Controller class, function trying to set session

function checkDatabase($password){
  $this->load->model('login_model');

  $username = $this->input->post('username');

  $result = $this->login_model->loginUser($username, $password);

  if($result)
  {
    $sess_array = array();
     foreach($result as $row)
     {
       $sess_array = array(
         'id' => $row->id,
         'username' => $row->username
       );

    $this->session->set_userdata($sess_array);
  }
     return TRUE;
  }
  else
  {
    return false;
  }
}
}

Finally a trying to retrieve session data

  <?php
   defined('BASEPATH') OR exit('No direct script access allowed');

   class Dashboard extends CI_Controller {


 public function index()
 {

    $data['title'] = 'Dashboard';

    $data['username'] = $this->session->userdata('username');


    $this->load->view('template/header', $data);
    $this->load->view('template/navigation');
    $this->load->view('dashboard_view',$data);
    $this->load->view('template/footer');
}
}

I am loading the session library in the autoload.php file that's why I'm not in each of the functions. In dashboard_view I echo $username but nothing is displayed.

Any ideas?

EDIT

Dashboard Controller

  <?php
  defined('BASEPATH') OR exit('No direct script access allowed');

  class Dashboard extends CI_Controller {


public function index()
{

    $data['title'] = 'Dashboard';

    $session_data = $this->session->userdata();

    print_r($session_data);
    die;

   $user = $session_data['username'];


    $this->load->view('template/header', $data);
    $this->load->view('template/navigation');
    $this->load->view('dashboard_view',$data);
    $this->load->view('template/footer');
}
}

Upvotes: 1

Views: 648

Answers (1)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

Try this

In Controller

function checkDatabase($password)
{
    $this->load->model('login_model');

    $username = $this->input->post('username');

    $result = $this->login_model->loginUser($username, $password);

    if($result != false)
    {
        $sess_array = array(
            'id' => $result[0]['id'],
            'username' => $result[0]['userName']
        );

        $this->session->set_userdata($sess_array);

        print_r($this->session->userdata());
        die;
    }
    else
    {
        return false;
    }
}

In Model

function loginUser($username, $password)
{
    $this->db->select('id, userName, password');
    $this->db->from('user_tbl');
    $this->db->where('userName', $username);
    $this->db->where('password', $password);
    $this->db->limit(1);

    $query = $this->db->get();

    if($query -> num_rows() == 1)
    {
        return $query->result_array();
    }
    else
    {
        return false;
    }
}

Upvotes: 0

Related Questions