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

[eluser]davy_yg[/eluser]
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

Code:
<?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

Code:
<html>

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

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

</div>




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

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.
#2

[eluser]CroNiX[/eluser]
I don't see an opening or closing form tag, nor a head or body tag in your html, or a proper html declaration. In other words, most of this html is invalid and probably won't behave the same across different browsers. And you definitely need a form open tag as that tells the browser what url to send the data to, which should be your process method in the clogin controller.

BTW
Code:
$username = $this->input->post('username', TRUE);
is the same as
Code:
$username = $this->security->xss_clean($this->input->post('username'));
#3

[eluser]davy_yg[/eluser]

ok, I revise the my views into:

views/admin/login.php

Code:
&lt;html&gt;
&lt;head&gt;
&lt;link href="&lt;?php echo $logincss; ?&gt;"  rel="stylesheet" type="text/css" media="screen"&gt;
&lt;/head&gt;

&lt;body&gt;
<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');
  
   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);
  
   form_close();
   ?&gt;<br>
  
</div>

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

</div>

&lt;/body&gt;


&lt;/html&gt;

still nothing happen if I press the login button.
#4

[eluser]Tpojka[/eluser]
Try with capitals as said in user guide:
Code:
$this->load->model('Login_model');
// Validate the user can login
$result = $this->Login_model->validate();
#5

[eluser]davy_yg[/eluser]

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

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

which is the first page or the first home page, instead of the admin page (views/admin/admin.php).
#6

[eluser]Tpojka[/eluser]
Try with:
Code:
echo form_open('admin/clogin/process');
#7

[eluser]CroNiX[/eluser]
Your submit button isn't within the form, so it obviously won't do anything.
#8

[eluser]davy_yg[/eluser]
I place the form_submit() after the form_close() and change form_open('admin/clogin/process');

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);

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;





Theme © iAndrew 2016 - Forum software by © MyBB