0
votes

if I do not fill in the username or username is not in the database then it will be error Trying to get property 'password' of non-object

Filename: controllers/user.php

Line Number: 74`

my code controller/user

  public function login(){
    $this->load->library('form_validation');
    $input = $this->input->post(NULL, TRUE);
    $this->username_temp = @$input['username'];


    if($this->form_validation->run('login') == FALSE){
        $this->load->view('form_login');

    }
    else{
        $this->load->library('session');
        $login_data = array(
            'username' => $input['username'],

            'login_status' => TRUE
            );
        $this->session->set_userdata($login_data);
        redirect(base_url('depan/index'));
    }

}
public function password_check($str){
    $this->load->model('user_model');
    $user_detail = $this->user_model->get_user_detail($this->username_temp);
    if($user_detail){
        if($user_detail->password == crypt($str,$user_detail->password)){
            return TRUE;
        }
        else{
            $this->form_validation->set_message('password_check', 'Password 
yang anda masukan salah');
            return FALSE;
        }
    }
    else{
        $this->form_validation->set_message('password_check', 'Username 
yanga anda masukan ');
            return FALSE;
    }



}

my code form validation in config

'login' => array(
    array('field' => 'username',
            'label' => 'username',
            'rules' => 'required'

        ),
    array(  'field' => 'password',
            'label' => 'password',
            'rules' => 'required|callback_password_check'
            )

    )

my code form_login

<h1>Login</h1>
    <?php echo validation_errors(); ?>
    <form action="<?php echo base_url('user/login'); ?>" method="POST">
        <label>Username</label><input type="text" name="username" /><br />
        <label>Password</label><input type="password" name="password" /><br />
        <input type="submit" name="submit" value="Login" />

my user_detail in model

public function get_user_detail($username){
    $this->db->where("username", $username);
    $query = $this->db->get('register');

    if($query->num_rows() > 0){
        $data = $query->row();`enter code here`
        $query->free_result();

    }

    else{
            $data = NULL;
    }
    return $data;
}

why if I do not fill the username in the form will error?

Message: Trying to get property 'password' of non-object

Filename: controllers/user.php

Line Number: 74

Backtrace:

File: D:\XAMPP\htdocs\ilmu\ilmusepuluh\application\controllers\user.php Line: 74 Function: _error_handler

line number 74 is

$this->form_validation->set_message('password_check', 'Password yang anda masukan salah');

1
The only place in the code you show where password is used as a property is if($user_detail->password == crypt($str,$user_detail->password)){ Is that line 74? If not, what is the code of line 74?DFriend
Kita sama-sama orang Indonesia, bisakah anda menjelaskan kode yang mana yang merupakan line number 74?Gilang Pratama
"if I do not fill in the username or username is not in the database" the first condition doesn't make sense as you have username as required so you should get a The username field is required. Irregardless, I ran your same code on my platform and wasn't able to replicate the error.Alex
@GilangPratama please keep everything in English ;)Alex
@Alex Yes sir, I will.Gilang Pratama

1 Answers

0
votes

sorry the error is on my own, I incorrectly enter the name of the folder in the browser. should be http: // localhost / science / ilmusebelas / user / login not http: // localhost / science / ilmusepuluh / user / login sorry very sorry, the above code is correct