Hello,

I am trying to create admin login page. This is what I try so far:

controllers/admin/clogin.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: Jorge Torres
* Description: Login controller class
*/
class Clogin extends CI_Controller{

    function __construct(){
        parent::__construct();
    }

    public function index(){
        // Load our view to be displayed
        // to the user

        $this->data['assets'] = array('logincss' => base_url().'assets/css/login.css',
                                'logo2' => base_url().'assets/images/logo2.png'
                                );

        $this->load->view('admin/login', $this->data['assets']);
    }

     public function process(){
        // Load the model
        $this->load->model('Login_model');
        // Validate the user can login
        $result = $this->Login_model->validate();
        // Now we verify the result
        if(! $result){
            // If user did not validate, then show them login page again
            $this->index();
        }else{
            // If user did validate,
            // Send them to members area
            redirect('admin');
        }        
    }
}
?>

models/login_model.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: Jorge Torres
* Description: Login model class
*/
class Login_model extends CI_Model{
    function __construct(){
        parent::__construct();
    }

    public function validate(){
        // grab user input

        $ids_user = $this->input->post('ids_user', TRUE);  

        // Prep the query
        $this->db->get_where('ids_user', $username);       

        // Run the query
        $query = $this->db->get('ids_user');
        // Let's check if there are any results
        if($query->num_rows() == 1)
        {
            // If there is a user, then create session data
            $row = $query->row();
            $data = array(
                    'username' => $row->username,
                    'password' => $row->password,
                    'validated' => true
                    );
            $this->session->set_userdata($data);
            return true;
        }
        // If the previous process did not validate
        // then return false.
        return false;
    }
}
?>

views/admin/login.php

<html>
<head>
<link href="<?php echo $logincss; ?>"  rel="stylesheet" type="text/css" media="screen">
</head>

<body>
<div id="logoadmin"></div>

<div id="loginbox">

<img src="<?php echo $logo2; ?>" width="150">
<br><br>

<div id="username">
<br><br>
<div id="position">

<?php      

      echo form_open('admin/clogin/process');

      echo form_label('Username:  ', 'username');

      $dataUsername = array(
              'username1'    => 'username1',
              'id'          => 'username1',
              'value'       => '',
              'size'        => '10',
              'style'       => 'width:43%'
            );

      echo form_input($dataUsername); ?><br>

<?php      

      echo form_label('Password:  ', 'password');

      $dataPassword = array(
              'password'    => 'password',
              'id'          => 'password',
              'value'       => '',
              'size'        => '10',
              'style'       => 'width:43%'
            );

      echo form_password($dataPassword);      

      ?><br>

</div>

<div id="submit">
<?php echo form_submit(array('value' => 'login', 'class' => 'button')); ?>
</div>

<?php form_close(); ?>
</div>

</div>

</body>


</html>

This is my table:

ids_user

username
password
user_email
user_fname
user_address
user_phone

After I login:

I brings me to this url : http://localhost/IndonusaCI/index.php?admin/clogin/process (the front page / home page)

If type this url : http://localhost/IndonusaCI/index.php/admin/clogin/process

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Clogin::$db

Filename: core/Model.php

Line Number: 51

Fatal error: Call to a member function get_where() on a non-object in C:\xampp\htdocs\IndonusaCI\application\models\login_model.php on line 18

line 18: $this->db->get_where('ids_user', $username);

why is it? how to fix the error?

looks like your database library is not loaded. You can autoload in the config/ directory in the autoload.php file.. you can add database to the $autoload['libraries'] array(). Or your can call the library by itself.

In my opinion your controller and models are a bit complicated. Here is how I build my logins.

admin.php:

    public function login(){

        $data['baseurl'] = $this->config->item('base_url');
        $data['sitename'] = $this->conifig->item('site_name');

        $this->load->view('admin/login', $data);    

    }


    public function authenticate(){
        //form validation library is autoloaded
        $this->form_validation->set_rules('username', 'username', 'required|trim|xss_clean');
        $this->form_validation->set_rules('password', 'password', 'required|trim|xss_clean');

        if($this->form_validation->run() == false){
            //Run Error Code
            $this->session->set_flashdata('username', $this->form_validation->error('username'));
            $this->session->set_flashdata('password', $this->form_validation->error('password'));

            redirect('login');

        }else{

            $authenticated = $this->admin_model->authenticate_user($this->input->post('username', true), sha1($this->input->post('password', true));

            if($authenticated == false){
                $this->session->set_flashdata('message', 'Error: Invalid username or password!');
                redirect('login');
            }else{
                $_SESSION['admin_username'] = $authenticated['username'];
                $_SESSION['admin_id']       = $authenticated['id'];
                redirect('dashboard');
            }
        }
    }

admin_model.php:

public function authenticate_user($username, $password){
    $query = $this->db->get_where('admins', array('username' => $username, 'password' => $password));

    if($query->num_rows() == 1){
        return $query->row_array();
    }else{
        return false;
    }
}

login.php

<?php echo form_open('admin/authenticate',array('id' => 'form-login')); ?>
<?php echo $this->session->flashdata('username'); ?>
<?php echo $this->session->flashdata('password'); ?>
<?php $atts = array('id' => 'username' ,'class' => 'input-text','name' => 'username', 'placeholder' => 'Enter username'); ?>
<?php echo form_input($atts); ?>
<?php $atts = array('id' => 'password' ,'class' => 'input-text','name' => 'password'); ?>
<?php echo form_password($atts); ?>
<?php echo form_submit('submit', 'Login'); ?>
<?php echo form_close(); ?>

How to route it? I try this: $route['admin/login'] = 'cadmin/login';

I type this: http://localhost/IndonusaCI2/cadmin/login or http://localhost/IndonusaCI2/admin/login

Then this error appears:

Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7

I believe you would need this for the urls you provided.

$route['admin/login'] = 'admin/login';
$route['cadmin/login'] = 'admin/login';

I have tried writting the routes and the result remains the same.

--------------------

I brings me to this url : http://localhost/IndonusaCI/index.php?admin/clogin/process (the front page / home page)

If type this url : http://localhost/IndonusaCI/index.php/admin/clogin/process

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Clogin::$db

Filename: core/Model.php

Line Number: 51

Fatal error: Call to a member function get_where() on a non-object in C:\xampp\htdocs\IndonusaCI\application\models\login_model.php on line 18

line 18: $this->db->get_where('ids_user', $username);

-------------------------

how to fix it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.