dongnan1899 2014-06-23 11:16
浏览 30

未使用自定义验证登录Codeigniter

In my codeigniter login I am not having any no luck logging on. I throws One error now called. Undefined property: error message. I also am not sure if my validate is correct if that is why not picking up user.

Error

Message: Undefined property: CI_DB_mysqli_driver::$num_rows

Library File

public function login() {
        $this->CI =& get_instance();
        $this->CI->load->library('session');
        $this->CI->load->library('security/encryption');


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

        $user_query = $this->CI->db->select('*')
        ->where('username', $username)
        ->where('password',  $this->CI->encryption->hash_password().hash('sha512', $password).$this->CI->encryption->generate_salt())
        ->where('status', "1")
        ->from('user');
        if($user_query->num_rows) {

              $data = array(
                    'ip' => $this->CI->input->ip_address(),
                    'user_id' => $this->CI->session->userdata('user_id')
              );

              $this->db->where('user_id', $user_id);
              $this->db->update('user', $data); 

              return true;

        } else {

              return false;

        }
  }

I use my own form validation method rather than codeigniter way. My way I have more control over it.

Controller

public function index() {
            $this->load->library('users');
            $this->lang->load('common/login', 'english')


            if(($this->input->server('REQUEST_METHOD') == 'POST') && $this->validate()) {

                  $data = array(
                      'username' => $this->input->post(),
                      'user_id' => $row->user_id,
                      'isLogged' => true
                  );
                  $this->db->get('user');
                  $this->session->set_userdata($data);

                  redirect('dashboard');
            }

            if (array_key_exists('warning', $this->error)) {
                  $data['error_warning'] = $this->error['warning'];
            } else {
                  $data['error_warning'] = '';
            }

            if (array_key_exists('username', $this->error)) {
                  $data['error_username'] = $this->error['username'];
            } else {
                  $data['error_username'] = '';
            }

            if (array_key_exists('password', $this->error)) {
                  $data['error_password'] = $this->error['password'];
            } else {
                  $data['error_password'] = '';
            } 

            $data['action'] = site_url("login");

            if (!"" == trim($this->input->post('username'))) {
                  $data['username'] = $this->input->post('username');
            } else {
                  $data['username'] = '';
            }

            if (!"" == trim($this->input->post('password'))) {
                  $data['password'] = $this->input->post('password');
            } else {
                  $data['password'] = '';
            }

            $this->load->view('template/common/login', $data);
      }

      protected function validate() {
            if (!"" == trim($this->input->post('username')) || !"" == trim($this->input->post('password')) || !$this->users->login($this->input->post('username'), $this->input->post('password'))) {
                  $this->error['warning'] = $this->lang->line('error_login');
            }

            return !$this->error;
      }
}
  • 写回答

1条回答 默认 最新

  • drws65968272 2014-06-23 12:17
    关注

    I'm not an expert in oop or in CodeIgniter but your library doesn't make much sense to me.

    Try doing var_dump() of your $user_query->num_rows, also the query looks like it's using CI active record so the query should look something like this

    // build the query
    $this->CI->db->select('*');
    $this->CI->db->from('user');
    $this->CI->db->where('username', $username);
    $this->CI->db->where('password',  $this->CI->encryption->hash_password().hash('sha512', $password).$this->CI->encryption->generate_salt());
    $this->CI->db->where('status', "1");
    
    // execute query
    $query = $this->CI->db->get();
    
    // do your thing
    if ( $query->num_rows() > 0 ) {
    
        $data_row = $query->row();
    
        $data = array(
                'ip' => $this->CI->input->ip_address(),
                'user_id' => $data_row->user_id // you don't have user_id in session
            );
    
        $this->db->where('user_id', $data_row->user_id); // same thing here for user_id
        $this->db->update('user', $data); 
    
        // You should start session here because you want to login the user :)
    
        return true;
    }
    

    I think this should do the trick. Also you were using user_id from session that doesn't exists. You are in process of loging in so you can't have user_id in it. This way we use result of query for it.
    You should consider of starting session if result is true and store some data in it.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题