[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);
}
}
}
?>
Here's the model
Code:
<?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;
}
}
}
?>
Here's the other controller for home
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller{
public function index()
{
$this->load->view('user_home');
}
}
?>
and the view for the validated user
Code:
<?php
$username = $this->session->userdata('username');
echo "Welcome " + $username;
$this->session->unset_userdata('username');
?>
I don't know which part I did is wrong