Hello,

I basically trying to create a login form that works that will carry me to the admin page after login. This is what I did so far:

models/admin/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
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));

        // Prep the query
        $this->db->where('username', $username);
        $this->db->where('password', $password);

        // Run the query
        $query = $this->db->get('username');
        // 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>

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

<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_label('Username:&nbsp&nbsp', 'username');

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

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

<?php      

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

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

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

</div>

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

</div>




</html>

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('home');
        }        
    }
}
?>

database: indonusaci

table: ids_user

username
password
user_email
user_fname
user_address
user_phone

What's wrong with my form? If I press login button nothing happen. What should I do to fix my codes? Thanks before.

Member Avatar for LastMitch

What's wrong with my form? If I press login button nothing happen. What should I do to fix my codes? Thanks before.

@davy_yg

Is this related to this:

http://www.daniweb.com/web-development/php/threads/464676/login-with-ci

I would suggest if you keep it to 1 thread or noone will bother reading your threads.

After read your code, I realzied this is the same code from 2 weeks ago?

It hasn't change much.

What have you done to make it work?

Do you understand how this works:

http://ellislab.com/codeigniter/user-guide/overview/mvc.html

Hello,

I try to make my login form works. Please help.

This is what I try so far:

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:&nbsp;&nbsp;', 'username');

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

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

<?php   

   echo form_label('Password:&nbsp;&nbsp;', '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> 

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/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

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

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

        // Run the query
        $query = $this->db->get('username');
        // 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;
    }
}
?> 

Now, after I login it starts to bring me to this url:

http://localhost/IndonusaCI/index.php?admin/clogin/process

Not the admin page.

I try typing 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 where() on a non-object in C:\xampp\htdocs\IndonusaCI\application\models\login_model.php on line 19

line 19: $this->db->where(‘username’, $username);

—————————

database: indonusaci
table: ids_user

username
password
user_email
user_fname
user_address
user_phone

————————-

how to fix the errors?

Member Avatar for LastMitch

how to fix the errors?

@davy_yg

It's your path connection. How do you put these files?

There shouldn't be a connection issue at all.

These files should be place in the folder that they are assigned.

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.