Welcome Guest, Not a member yet? Register   Sign In
User can't be found for some reason
#1

[eluser]xtremer360[/eluser]
I'm trying to figure out how to fix the issues that presented to the console when I submit my login form.

Login Controller:
http://pastebin.com/C24MBL46

Code:
array(1) {
  [0]=>
  object(stdClass)#27 (9) {
    ["user_id"]=>
    string(1) "1"
    ["username"]=>
    string(10) "xtremer360"
    ["password"]=>
    string(40) "some string"
    ["password_hash"]=>
    string(11) "some string"
    ["first_name"]=>
    string(7) "Jeffrey"
    ["last_name"]=>
    string(8) "Davidson"
    ["email_address"]=>
    string(20) "[email protected]"
    ["user_status_id"]=>
    string(1) "2"
    ["user_roles_id"]=>
    string(1) "1"
  }
}
<div>

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined variable: user_status_id</p>
<p>Filename: controllers/login.php</p>
<p>Line Number: 100</p>

</div><div>

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined variable: user_status_id</p>
<p>Filename: controllers/login.php</p>
<p>Line Number: 120</p>

</div><div>

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined variable: message</p>
<p>Filename: controllers/login.php</p>
<p>Line Number: 78</p>

</div>{"error":true,"message":null}
#2

[eluser]Aken[/eluser]
You're trying to extract something that's in the wrong format. Try having your get_user_data() user model method return something like this:

Code:
return $query->row_array();
#3

[eluser]Egill Th[/eluser]
Fix this by altering your code like so;
Code:
private function check_user_status_id( $user_status_id = 0 ) {
  switch ( $user_status_id ) {
    case 1:
      $message = 'Sorry you must verify your account before logging in!';
    break;
    case 3:
      $message = 'Your account has been suspended!';
    break;
    case 4:
      $message = 'Your account is currently banned!';  
    break;
    case 5:
      $message = 'Your account has been deleted!';
    break;
  }
      
  return $message;
}

Regarding the $message error personally I would define the variable and use it like so;

Code:
var $message;
private function check_user_status_id( $user_status_id = 0 ) {
  switch ( $user_status_id ) {
    case 1:
      $this->message = 'Sorry you must verify your account before logging in!';
    break;
    case 3:
      $this->message = 'Your account has been suspended!';
    break;
    case 4:
      $this->message = 'Your account is currently banned!';  
    break;
    case 5:
      $this->message = 'Your account has been deleted!';
    break;
  }
      
  return $this->message;
}

Alternatively you can disable E_NOTICE errors but it is good practice to define your variables before using them.

To fix the fact that you aren't getting the proper $user_status_id you can edit your code to something like this;

Code:
public function submit() {
  if ( $this->form_is_valid() ) {
    $post_username = $this->input->post( 'username' );
    $post_password = $this->input->post( 'password' );
          
    $user_data = $this->users_model->get_user_data( $post_username );
    var_dump($user_data);
    
    if ( !empty($user_data) ) {
      if ( $user_data[0]->user_status_id == '2' ) {
        if ( !$this->is_max_login_attempts_exceeded( $post_username, $user_data ) ) {
          if ( $this->is_authenticated( $post_username, $post_password ) ) {
            $output_array = array( 'error' => FALSE, 'message' => 'Successful login! Going to the dashboard!' );
          } else {
            $output_array = array( 'error' => TRUE, 'message' => 'Incorrect username and password combination!' );                            
          }
        } else {
          $output_array = array( 'error' => TRUE, 'message' => 'Your account is currently locked, we appologize for the inconvienence. You must wait 30 minutes before you can login again! An email was sent to the owner of this account!' );
        }
      } else {
        $output_array = array( 'error' => TRUE, 'message' => $this->check_user_status_id( $user_data[0]->user_status_id ) );                    
      }
    } else {
      $output_array = array( 'error' => TRUE, 'message' => 'User was not found in the database!' );
    }
  } else {
    $output_array = array( 'error' => TRUE, 'message' => validation_errors() );
  }
      
  echo json_encode($output_array);
}
#4

[eluser]Egill Th[/eluser]
For some reason I posted another reply instead of updating my existing one. ^
#5

[eluser]xtremer360[/eluser]
I updated my code. What I'm trying to do is always work with objects and not arrays but I am getting a trying to get property of a non object on the login function in the list of functions and where it says $user_data->password_hash AND $user_data->password just a few lines down from that.

http://pastebin.com/BkkTp2cG

Code:
/**
     * Mylib::is_max_login_attempts_exceeded()
     *
     * @param mixed $post_username
     * @return boolean
     */
    public function is_max_login_attempts_exceeded( $post_username )
    {
        $login_attempts = $this->ci->users_model->get_login_attempts_number( $post_username );
        return $login_attempts >= 5 ? TRUE : FALSE;
    }
    
    public function get_user_data( $post_username )
    {
        $user_data = $this->ci->users_model->get_user_data( $post_username );
        if( is_object( $user_data ) )
        {
            $user_data = get_object_vars( $user_data );
            return $user_data;
        }
        return NULL;
    }
    
    public function login( $post_username, $post_password, $user_data )
    {
        $regen_post_password = $this->ci->genfunc->reGenPassHash( $post_password, $user_data->password_hash );
        
        if ( $regen_post_password == $user_data->password )
        {
            $profile_data = $this->ci->users_model->get_profile_data( $user_data->user_id );
            $this->ci->session->set_userdata( array( 'xtr' => 'yes', 'user_id'  => $user_data->user_id, 'username' => $user_data->username, 'role' => $user_data->user_roles_id, 'default_roster_id' => $profile_data->default_roster_id ) );
            $this->clear_login_attempts( $post_username );
            $this->ci->users_model->insert_session( $this->ci->session->userdata( 'session_id' ), $this->ci->session->userdata( 'user_id' ), $this->ci->input->ip_address(), $this->ci->session->userdata( 'user_agent' ));
            return $user_data;
        }
        else
        {
            $this->increase_login_attempt( $post_username );
            return NULL;
        }
    }

Code:
public function get_user_data( $username )
    {
        $this->db->where( 'username', $username );
        $query = $this->db->get( $this->master_model->users_table );
        return $query->num_rows() == 1 ? $query->row() : NULL;
    }
    
    public function get_login_attempts_number( $post_username )
    {
        $this->db->where( 'ip_address', $this->input->ip_address );
        $this->db->or_where( 'username', $post_username );
        $query = $this->db->get( $this->master_model->user_login_attempts_table );
        return $query->num_rows;
        
    }
#6

[eluser]Egill Th[/eluser]
Personally I would not check if the query is sane inside the model and ust return the result without any logic.

Code:
public function get_user_data($username) {
  $result = $this->db->get_where($this->master_model->users_table, array('username' => $username));
  return $result->result();
}

I'm not sure why you would want to convert the stdClass to something else, but you could always just do something like this;

Code:
$user_data = $this->ci->users_model->get_user_data($post_username);
$user_data = $user_data[0];

I typically use a custom Model class and when I select data from a single table I do something along these lines;

Code:
public function doStuff($id) {
  $this->load->model('usermodel'); // Typically this is loaded in the construct.
  
  // This returns an object with all the variables from the result
  // assigned to variables.
  $user = new Usermodel($id);
  
  $user->id;
  $user->user_role_id;
  $user->username;
}
To do this all you need to do is create a core class to be extended by all model classes that you want to do this with.

There are multiple database ORMs available for CodeIgniter that may possibly do this for you or you can write your own simple class.




Theme © iAndrew 2016 - Forum software by © MyBB