I want a login solution in CodeIgniter. I want code for user login, (the user who is registered) I'm using Mysql with php and working on localhost Kindly tell me that how can I make a login controller and model for login I'm attaching my register.php so may be you understand what should I code for login.
I updated my question. Here I'm attaching my updated files
login.php (controller)
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Login extends CI_Controller {
function index() {
$myData['pageName'] = 'login';
$this->load->helper(array('form', 'url'));
$this->load->database();
if ($_POST) {
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == false) {
// Login page view...
$this->load->view('login', $myData);
}
else{
}
$this->load->model('Login');
if ($this->Login->check_login_details($this->input->post('email') , $this->input->post('password'))) {
// show success
$this->load->view('success_login', $myData);
}
}else {
// Invalid details...
$this->load->view('login', $myData);
}
}
}
?>
Core Model where i add function check_details
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class MY_Model extends CI_Model {
const TableName = 'form';
const TablePK = 'id';
public function ins_record() {
$this->db->insert($this::TableName, $this);
}
public function getRec() {
$res = $this->db->get($this::TableName);
return $res->result_array();
}
public function get_userName($colName, $value) {
$res = $this->db->get_where($this::TableName, array($colName => $value));
return $res->result_array();
}
public function check_login_details($email, $password)
{
// Do whatever to the password...
$password = md5($password);
$query = $this->db->get_where($this::TableName, array('email' => $email, 'password' => $password));
return ($query->num_rows() > 0) ? true : false;
}
}
?>
this is Model of login.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Login extends My_Model{
const TableName = 'form';
const TablePK = 'id';
public $email = '';
public $password = '';
}
?>
here is also my login.php (view code)
<?php
include('./includes/header.php');
?>
<!-- MAIN -->
<div id="main">
<!-- wrapper-main -->
<div class="wrapper">
<!-- content -->
<div id="content">
<!-- title -->
<div id="page-title">
<span class="title">Sign in Form</span>
<span class="subtitle">Kindly login here to access site's useful features</span>
</div>
<!-- ENDS title -->
<form class="form-signin" role="form" method="post">
<h2 >Please sign in</h2>
<input type="email" class="form-control" placeholder="Email address" required autofocus>
<input type="password" class="form-control" placeholder="Password" required >
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
<!-- ENDS content -->
</div>
<!-- ENDS wrapper-main -->
</div>
<!-- ENDS MAIN -->
<!-- Twitter -->
<div id="twitter">
<div class="wrapper">
<a href="#" id="prev-tweet"></a>
<a href="#" id="next-tweet"></a>
<img id="bird" src="img/bird.png" alt="Tweets" />
<div id="tweets">
<ul class="tweet_list"></ul>
</div>
</div>
</div>
<!-- ENDS Twitter -->
<?php
include('./includes/footer.php');
?>
Now please tell me what code should be used for successful login that fetch data from database