Welcome Guest, Not a member yet? Register   Sign In
[Solved] Get Instance Not Working In Library
#1

(This post was last modified: 07-04-2015, 06:14 AM by wolfgang1983.)

When I submit my form codeigniter get instance CI not working. Throws error below? I use HMVC.

Severity: Notice
Message: Undefined variable: CI
Filename: libraries/User.php
Line Number: 17

Line 17 is $input_password = $CI->input->post('password');

Any ideas on whats wrong with library?

PHP Code:
<?php

class User {

    private 
$user_id;
    private 
$user_group_id;
    private 
$username;
    private 
$firstname;
    private 
$lastname;
    private 
$email;

    public function 
__construct() {
        
$CI =& get_instance();
    }

    public function 
login() {
        
$input_password $CI->input->post('password');

        if (
$this->validate_password($input_password)) {

            
$CI->db->where('username'$CI->input->post('username'));
            
$user_query $CI->db->get($CI->db->dbprefix 'user');

            if (
$user_query->num_rows() > 0) {

                
$row $user_query->row();

                
$CI->session->set_userdata(array('user_id' => $row->user_id));

                
$this->user_id $row->user_id;
                
$this->user_group_id $row->user_group_id;
                
$this->username $row->username;
                
$this->firstname $row->firstname;
                
$this->lastname $row->lastname;
                
$this->email $row->email;

                return 
true;

            } else {

                return 
false;    

            }

        } else {

            return 
false;
        }

    }

    public function 
get_user_id() {
        return 
$this->user_id;
    }

    public function 
get_user_group_id() {
        return 
$this->user_group_id;
    }

    public function 
get_user_username() {
        return 
$this->username;
    }

    public function 
get_user_firstname() {
        return 
$this->firstname;
    }

    public function 
get_user_lastname() {
        return 
$this->lastname;
    }

    public function 
get_user_email() {
        return 
$this->email;
    }

    public function 
validate_password($password) {
     
   if (password_verify($password$this->stored_hash())) {
     
       return $password;
     
   } else {
     
       return false;
     
   }
    }

    public function 
stored_hash() {
     
   $CI->db->where('username'$CI->input->post('username')); 
     
   $query $CI->db->get($CI->db->dbprefix 'user');

     
   if ($query->num_rows() > 0) {
     
       $row $query->row();
     
       return $row->password;
     
   } else {
     
       return false;
     
   }
    }



Attached Files
.php   User.php (Size: 1.87 KB / Downloads: 151)
.php   login_view.php (Size: 1.35 KB / Downloads: 247)
.php   Login.php (Size: 1,014 bytes / Downloads: 199)
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

(07-04-2015, 05:56 AM)riwakawd Wrote: When I submit my form codeigniter get instance CI not working. Throws error below? I use HMVC.

Severity: Notice
Message: Undefined variable: CI
Filename: libraries/User.php
Line Number: 17

Line 17 is $input_password = $CI->input->post('password');

Any ideas on whats wrong with library?


PHP Code:
<?php

class User {

    private 
$user_id;
    private 
$user_group_id;
    private 
$username;
    private 
$firstname;
    private 
$lastname;
    private 
$email;

    public function 
__construct() {
        
$CI =& get_instance();
    }

    public function 
login() {
        
$input_password $CI->input->post('password');

        if (
$this->validate_password($input_password)) {

            
$CI->db->where('username'$CI->input->post('username'));
            
$user_query $CI->db->get($CI->db->dbprefix 'user');

            if (
$user_query->num_rows() > 0) {

                
$row $user_query->row();

                
$CI->session->set_userdata(array('user_id' => $row->user_id));

                
$this->user_id $row->user_id;
                
$this->user_group_id $row->user_group_id;
                
$this->username $row->username;
                
$this->firstname $row->firstname;
                
$this->lastname $row->lastname;
                
$this->email $row->email;

                return 
true;

            } else {

                return 
false;    

            }

        } else {

            return 
false;
        }

    }

    public function 
get_user_id() {
        return 
$this->user_id;
    }

    public function 
get_user_group_id() {
        return 
$this->user_group_id;
    }

    public function 
get_user_username() {
        return 
$this->username;
    }

    public function 
get_user_firstname() {
        return 
$this->firstname;
    }

    public function 
get_user_lastname() {
        return 
$this->lastname;
    }

    public function 
get_user_email() {
        return 
$this->email;
    }

    public function 
validate_password($password) {
     
   if (password_verify($password$this->stored_hash())) {
     
       return $password;
     
   } else {
     
       return false;
     
   }
    }

    public function 
stored_hash() {
     
   $CI->db->where('username'$CI->input->post('username')); 
     
   $query $CI->db->get($CI->db->dbprefix 'user');

     
   if ($query->num_rows() > 0) {
     
       $row $query->row();
     
       return $row->password;
     
   } else {
     
       return false;
     
   }
    }


I got it working couple things I missed spelled in on of the config settings.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#3

I don't know how you got it working, but $CI should be $this->CI everywhere you're using it since CI is a property of your class.
$input_password = $this->CI->input->post('password');
Reply
#4

(This post was last modified: 07-04-2015, 08:04 PM by wolfgang1983.)

(07-04-2015, 12:26 PM)CroNiX Wrote: I don't know how you got it working, but $CI should be $this->CI everywhere you're using it since CI is a property of your class.
$input_password = $this->CI->input->post('password');

@CroNiX I have now updated it your way and works even better.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#5

(07-04-2015, 12:26 PM)CroNiX Wrote: I don't know how you got it working, but $CI should be $this->CI everywhere you're using it since CI is a property of your class.
$input_password = $this->CI->input->post('password');

Magic. Sometimes it happens.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB