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

[eluser]davy_yg[/eluser]

Hello,

I am trying to create login page with CI. Can anyone help me check this codes:

user
----------------
user_name
user_pass


model/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('user_name'));
        $password = $this->security->xss_clean($this->input->post('user_pass'));
        
        // Prep the query
        $this->db->where('user_name', $username);
        $this->db->where('user_pass', $password);
        
        // Run the query
        $query = $this->db->get('users');
        // 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;
    }
}
?>

controller/admin/clogin.php

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: Jorge Torres
* Description: Login controller class
*/
class Login extends CI_Controller{
    
    function __construct(){
        parent::__construct();
    }
    
    public function index(){
        // Load our view to be displayed
        // to the user
        $this->load->view('login_view');
    }

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

views/admin/login.php

Code:
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
&lt;html  xml:lang='en' lang='en'&gt;

&lt;head&gt;    
    &lt;title&gt;Jotorres Login Screen | Welcome &lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    <div id='login_form'>
        &lt;form acti echo base_url();?&gt;login/process' method='post' name='process'&gt;
            <h2>User Login</h2>
            <br />            
            <label for='username'>Username</label>
            &lt;input type='text' name='username' id='username' size='25' /&gt;&lt;br />
        
            <label for='password'>Password</label>
            &lt;input type='password' name='password' id='password' size='25' /&gt;&lt;br />                            
        
            &lt;input type='Submit' value='Login' /&gt;            
        &lt;/form&gt;
    </div>
&lt;/body&gt;
&lt;/html&gt;


Also, how to write the routes so that the codes work?


Messages In This Thread
login - by El Forum - 10-09-2013, 01:49 AM
login - by El Forum - 10-15-2013, 07:27 PM
login - by El Forum - 10-16-2013, 12:43 AM



Theme © iAndrew 2016 - Forum software by © MyBB