In this section, We'll understand how to create simple login form in CodeIgniter Framework.
We'll check customer's email and password, if it is valid then redirect on dashboard otherwise display an error message on login page.
In this section, We'll understand how to create simple login form in CodeIgniter Framework.
We'll check customer's email and password, if it is valid then redirect on dashboard otherwise display an error message on login page.
Using a text editor, create a controller called Login.php. In it, place this code and save it to your application/controllers/
directory:
<?php
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
/* Load form validation library */
$this->load->library('form_validation');
/* Validation rule */
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('login_form');
}
else {
$this->load->model('login_model');
$result = $this->login_model->login();
if ($result > 0)
redirect('login/dashboard');
else
{
$msg = "The email or password you entered is incorrect.";
$this->load->view('login_form', compact('msg'));
}
}
}
public function dashboard()
{
$this->load->view('dashboard');
}
}
?>
Using a text editor, create a form called login_form.php. In it, place this code and save it to your application/views/
directory:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="col-md-2"></div>
<div class="col-md-8" style="margin-top:20px">
<?php
echo form_open('login/index');
echo validation_errors();
if (isset($msg))
echo '<p>'.$msg.'</p>';
?>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" id="email" name="email" value="<?php echo set_value('email'); ?>" />
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" />
</div>
<button type="submit" class="btn btn-success">Submit</button>
<?php
echo form_close();
?>
</div>
<div class="col-md-2"></div>
</body>
</html>
Using a text editor, create a another view page called dashboard.php. In it, place this code and save it to your application/views/
directory:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h3> Welcome to dashboard !</h3>
</div>
</body>
</html>
Using a text editor, create a model called Login_model.php. In it, place this code and save it to your application/models/
directory:
<?php
class Login_model extends CI_Model
{
public function login()
{
$email = $this->input->post('email');
$password = md5($this->input->post('password'));
$query = $this->db->where(['email' => $email, 'password' => $password])->get('tbl_customers');
return (int) $query->num_rows();
}
}
To try your registration form, visit your site using a URL similar to this one:
http://localhost/codeigniter/index.php/login/index