CodeIgniter Forums
Help with session data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Help with session data (/showthread.php?tid=60088)



Help with session data - El Forum - 01-13-2014

[eluser]Unknown[/eluser]
Hi,

I'm new to CI and MVC. I need help with session data. I'm creating a login page and when user log in is successful it will display "Welcome username". I'm storing the users username session from the model then access the data in the view. The view however, displays "0".

Here's the controller

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller{

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

  public function index($msg = NULL)
  {
   $data['msg'] = $msg;
   $this->load->view('login_view', $data);
  }

  public function check_user()
  {
   $this->load->model('users_model');
   $result = $this->users_model->getUser();

   if($result)
   {
    redirect('home');
   }
   else
   {
    $msg = '<center><font color=red>Invalid username and/or password.</font><br /></center>';
    $this->index($msg);
   }
  }
}
?&gt;

Here's the model

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users_model extends CI_Model{

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

  public function getUser()
  {
   $username = $this->input->post('username');
   $password = $this->input->post('password');

   $this->db->where('username', $username);
   $this->db->where('password', MD5($password));

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

   if($query->num_rows() == 1)
   {
    $row = $query->row();
    $username= $row->username;
    $this->session->set_userdata('username', $username);
    return TRUE;
   }
   else{
    return FALSE;
   }
  }
}
?&gt;

Here's the other controller for home

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller{

  public function index()
  {
   $this->load->view('user_home');
  }
}
?&gt;

and the view for the validated user

Code:
&lt;?php
$username = $this->session->userdata('username');
echo "Welcome " + $username;
$this->session->unset_userdata('username');
?&gt;

I don't know which part I did is wrong


Help with session data - El Forum - 01-13-2014

[eluser]Unknown[/eluser]
I'm sorry I got it resolved. I'm so dumb..