Welcome Guest, Not a member yet? Register   Sign In
Login2
#1

[eluser]davy_yg[/eluser]

Hello,

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

This is what I try so far:

views/admin/login.php

Code:
<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="&lt;?php echo $logo2; ?&gt;" width="150">
<br><br>

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

&lt;?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); ?&gt;<br>

&lt;?php  
  
   echo form_label('Password:&nbsp;&nbsp;', 'password');
  
   $dataPassword = array(
              'password'    => 'password',
              'id'          => 'password',
              'value'       => '',
              'size'        => '10',
              'style'       => 'width:43%'
            );

   echo form_password($dataPassword);  
  
   ?&gt;<br>
  
</div>

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

&lt;?php form_close(); ?&gt;
</div>

</div>

&lt;/body&gt;


&lt;/html&gt;

controllers/admin/clogin.php

Code:
&lt;?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');
        }        
    }
}
?&gt;

models/login_model.php

Code:
&lt;?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;
    }
}
?&gt;

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

http://localhost/IndonusaCI/index.php?ad...in/process

Not the admin page.

I try typing this url: http://localhost/IndonusaCI/index.php/ad...in/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?
#2

[eluser]Tpojka[/eluser]
You didn't pass the data. Right way is view->controller->model.
You passed data from view with function
Code:
form_open('admin/clogin/process');
, but in controller you have to use same data too for passing it to model.
So it should be something like:
Code:
public function process(){
        
        $username = $this->input->post('username', TRUE);  
        $password = $this->input->post('password', TRUE);

        // Load the model
        $this->load->model('Login_model');
        // Validate the user can login
        $result = $this->Login_model->validate($username, $password); // here you send data to model
        // 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');
        }        
}
, and in model you have to adopt those data in form of method arguments:
Code:
public function validate($username, $password){ // same data you have sent from controller
  
        // 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;
    }
}

This should be the basic MVC principle and how is used in CI.

Check what have I changed and test it as well. I didn't read rest of the code.
#3

[eluser]davy_yg[/eluser]

When I try to login I receive this error message:

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

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

how to fix the error?
#4

[eluser]Tpojka[/eluser]
Do you have database library autoloaded since it is not in model nor controller?
#5

[eluser]davy_yg[/eluser]

A Database Error Occurred

Error Number: 1146

Table 'surveillanceci.username' doesn't exist

SELECT * FROM (`username`) WHERE `0` IS NULL

Filename: C:\xampp\htdocs\surveillanceCI\system\database\DB_driver.php

Line Number: 330

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

The table name is not username but svc_user . which codes that cause errors?
#6

[eluser]Tpojka[/eluser]
Quote:I didn't read rest of the code.
Code:
$query = $this->db->get('indonusaci');
#7

[eluser]davy_yg[/eluser]

The database name is surveillanceci. I try to update the database name to:

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

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

A Database Error Occurred

Error Number: 1146

Table 'surveillanceci.username' doesn't exist

SELECT * FROM (`username`) WHERE `0` IS NULL

Filename: C:\xampp\htdocs\surveillanceCI\system\database\DB_driver.php

Line Number: 330


The same error still appears.
#8

[eluser]Tpojka[/eluser]
[quote author="davy_yg" date="1382927914"]
database: indonusaci
table: ids_user

username
password
user_email
user_fname
user_address
user_phone

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

how to fix the errors?[/quote]

Switch `username` to `ids_user`.
#9

[eluser]davy_yg[/eluser]

I change username to svc_user. The same error still appears.
-------------------------------------------------

Error Number: 1146

Table 'surveillanceci.username' doesn't exist

SELECT * FROM (`username`) WHERE `0` IS NULL

Filename: C:\xampp\htdocs\surveillanceCI\system\database\DB_driver.php

Line Number: 330
#10

[eluser]Tpojka[/eluser]
Find queries in code.
Check again all code in this order:

1. view (not likely error is here)
2. controller
3. model (maybe here)

Something you should spot the place where is plaaced something that begin with SELECT * FROM (`username`) WHERE

No other way.




Theme © iAndrew 2016 - Forum software by © MyBB