-1

I'm trying to make a login system using codeigniter but there is a fatal error found when trying to login :(

Controler Auth.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

function __construct(){
    parent::__construct();
    $this->load->model('Auth/login','',TRUE);
}

class Auth extends CI_Controller {

    public function index()
    {
        // $this->load->view('Home');
    }

    // start login
   public function login()
   {
      $this->load->helper(array('form'));
      $this->load->view('Login');
   }
   // end login

   // start logout
   public function logout()
   {
       $this->session->unset_userdata('logged_in');
       redirect('Login','refresh');
   }
   // end logout


// start checkLogin
public function checkLogin()
{
    // field validation successfull, validate against database 
    $username = $this->input->post('username');
    $password = $this->input->post('password');
    // query database
    $this->load->model('Auth');
    $result = $this->Auth->login($username, $password);

    if ($result) {
        $sess_array = array();
        foreach ($result as $row) {
            $sess_array = array(
                $id = $row->id,
                $username = $row->username
            );
            $this->session->set_userdata('logged_in',$sess_array);
        }
        return TRUE;
    }else {
        $this->form_validation->set_message('checkLogin', 'Invalid username or password');
        return FALSE;
    }
}
// end checkLogin

}

Models Auth.php

<?php

class Auth extends CI_Model
{
    // start login
    function login($username, $password)
    {
        $this->db->select('id','username','password');
        $this->db->from('user_details');
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $this->db->limit(1);

        $query = $this->db->get();

        if ($query->num_rows() == 1) {
            return $query->result();
        }else{
            return false;
        }
    }
    // end login
}
?>

please help me to find the exact error I have done :'(

Indrasinh Bihola
  • 2,094
  • 3
  • 23
  • 25

1 Answers1

1

The problem is you have two class with same name Auth one for controller and one for model.One script cannot contain two class that's why you got that error.

Rename your auth model like this Auth_model.php and declare class like this

class Auth_model extends CI_Model

Hope you can do rest how to use this model.

Shaiful Islam
  • 7,034
  • 12
  • 38
  • 58
  • Thanks but now it's give another error :( **Fatal error: Call to a member function set_userdata() on null in** – Akbar Hossain Robin Jul 04 '15 at 12:32
  • check the following errors please https://nimbus.everhelper.me/client/notes/share/243095/2FzOriP5eFBZsTInOMBN9fGAPOF9FIUe/ – Akbar Hossain Robin Jul 04 '15 at 12:34
  • If you have different question you should ask another question or you can search for it at SO.`Fatal error: Call to a member function set_userdata()` means you using `Session` class but you did not load it. – Shaiful Islam Jul 04 '15 at 12:48