Welcome Guest, Not a member yet? Register   Sign In
Help with session data
#1

[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
#2

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




Theme © iAndrew 2016 - Forum software by © MyBB