Search code examples
phpcodeignitermysqlimport

UserId is not displaying after successfull login in codeigniter


I am new to codeigniter I Have create a login form and i am able to login succesfully fine and after login i am getting the login username also fine.but when i try to get the userid it is showing empty.I am displaying userid just like username displaying .But it is displaying empty why.Please any help would be appreciated thanks in Advance.

This is my model

class Login_Model extends CI_Model{
public function checklogin($uname,$pass){
  $this->db->select('id','username','password');
  $this->db->get('login');
  $this->db->where('username',$uname);
  $this->db->where('password',$pass);
  $query=$this->db->get('login');
  if($query->num_rows()==1){
    return true;
  }else{
    return false;
  }

}

This is mycontroller

 public function verifyuser(){
    $name=$this->input->post('username');
    $password=$this->input->post('password');
    $this->load->model('Login_Model');
  if($this->Login_Model->checklogin($name,$password)){
    $this->Login_Model->checklogin($name,$password);
    $this->session->set_userdata('username', $name);
    $this->session->set_userdata('id', $id);
    return true;
  }else{
    $this->form_validation->set_message('verifyuser','username or password is invalid');
    return false;
  }

}

This view

 $id= $this->session->userdata('id');
  echo "login id is".$id;

in View page i am accessing data like this but it giving empty for id but it is username fine.


Solution

  • It looks like you are just checking if the username and password match in the database in the checklogin function.

    You are not returning the id and storing it in $id variable.

    Hence since $id is undefined, no value is printed.

    Changes required

    Model Fetch the id from the database and return it instead of a boolean.

    class Login_Model extends CI_Model{
        public function checklogin($uname,$pass){
            $this->db->select('id','username','password');
            $this->db->get('login');
            $this->db->where('username',$uname);
            $this->db->where('password',$pass);
            $query=$this->db->get('login');
            if($query->num_rows()==1){
                $res = $query->row_array();
                return $res['id'];
            } else {
                return false;
            }
        }
    }
    

    Controller Fetch the id and use it to set the userdata.

    public function verifyuser(){
        $name=$this->input->post('username');
        $password=$this->input->post('password');
        $this->load->model('Login_Model');
        $id = $this->Login_Model->checklogin($name,$password);
        if($id){
            $this->session->set_userdata('username', $name);
            $this->session->set_userdata('id', $id);
            return true;
        } else {
            $this->form_validation->set_message('verifyuser','username or password is invalid');
            return false;
        }
    }