[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'>
<html xml:lang='en' lang='en'>
<head>
<title>Jotorres Login Screen | Welcome </title>
</head>
<body>
<div id='login_form'>
<form acti echo base_url();?>login/process' method='post' name='process'>
<h2>User Login</h2>
<br />
<label for='username'>Username</label>
<input type='text' name='username' id='username' size='25' /><br />
<label for='password'>Password</label>
<input type='password' name='password' id='password' size='25' /><br />
<input type='Submit' value='Login' />
</form>
</div>
</body>
</html>
Also, how to write the routes so that the codes work?