Search code examples
phpcodeigniterdeploymentcodeigniter-3

Codeigniter 3 login code working well at localhost but not working when deploying at domain


I've launch a website built with codeigniter 3 and i've made configuration changes such as base url, database, etc. Here is the error:

A PHP Error was encountered Severity: Warning Message: Undefined property: Login::$login Filename: controllers/Login.php Line Number: 11 Backtrace: File: /home/u753220969/domains/ngapaklaptop.shop/public_html/webngapak/application/controllers/Login.php Line: 11 Function: _error_handler File: /home/u753220969/domains/ngapaklaptop.shop/public_html/webngapak/index.php Line: 321 Function: require_once

An uncaught Exception was encountered Type: Error Message: Call to a member function getDefaultValues() on null Filename: /home/u753220969/domains/ngapaklaptop.shop/public_html/webngapak/application/controllers/Login.php Line Number: 11 Backtrace: File: /home/u753220969/domains/ngapaklaptop.shop/public_html/webngapak/index.php Line: 321 Function: require_once

Here is the controller:

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Login extends MY_Controller
{
    
    
    public function index()
    {
        if (!$_POST) {
            $input = (object) $this->login->getDefaultValues();
        } else {
            $input = (object) $this->input->post(null, true);
        }

        if (!$this->login->validate()) {    
            $data['title'] = 'NgapakLaptop | Sign In';
            $data['input'] = $input;

            $this->load->view('partial/header', $data);
            $this->load->view('partial/navbar');
            $this->load->view('landingpage/login', $data);
            $this->load->view('partial/footer');
            return;
        }

        if ($this->login->run($input)) {

            redirect(base_url('errors'));
        } else {
            $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert"> Gagal login. Cek email dan password Anda.</div>');
            redirect(base_url('login'));
        }
    }
}

Here is the model:

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Login_model extends MY_Model
{
    protected $table = 'user';  // Tabel ditentukan manual karena nama class model bukan nama tabel

    public function getDefaultValues()
    {
        return [
            'email'     => '',
            'password'  => ''
        ];
    }

    public function getValidationRules()
    {
        $validationRules = [
            [
                'field' => 'email',
                'label' => 'E-Mail',
                'rules' => 'trim|required|valid_email'
            ],
            [
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required'
            ]
        ];

        return $validationRules;
    }

    public function run($input)
    {
        $query = $this->where('email', strtolower($input->email))
            ->where('is_active', 1)
            ->first();
        
        if (!empty($query) && hashEncryptVerify($input->password, $query->password)) {
            // Jika user ada & password sama, maka buat session
            $sess_data = [
                'id'        => $query->id,
                'name'      => $query->name,
                'email'     => $query->email,
                'role'      => $query->role,
                'is_login'  => true
            ];
            
            $this->session->set_userdata($sess_data);
            return true;
        }

        return false;
    }
}

/* End of file Login_model.php */

and i made custom configuration on my core folder. Here is MY_Controller core

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        
        $model = strtolower(get_class($this));  // This mewakili file2 yang ada di folder Controllers
        // Apakah terdapat file model yang namanya sama dengan controller saat ini?
        if (file_exists(APPPATH . 'models/' . $model . '_model.php')) {
            // Param 1: load model, 2: nama controller - pasang model pada controller saat ini
            $this->load->model($model . '_model', $model, true);
        }
    }

    /**
     * Load view with default layouts
     
    public function view($data)
    {
        $this->load->view('layouts/app', $data);
    }
*/
}

/* End of file MY_Controller.php */

The code works perfectly on localhost but why do I get this error when I host to a domain?


Solution

  • I am not sure this will answer your question (it should be commented on but cannot due to text limitation)

    1. Seems $login Object Not Initialized

      • In your controller, you're trying to access $this->login->getDefaultValues(), but it seems $this->login is not initialized. Since your controller is Login and your model is Login_model, the model will be loaded with the name login_model, not login.

      Replace $this->login->getDefaultValues() to $this->login_model->getDefaultValues() in your Login controller.

    2. Linux hosts are case-sensitive

      • Make sure that all your file names and their references are used the same. For example, Login_model.php should look like this.
    3. Check file permission.

    These are the common factors that cause this issue.