In this article, we will discuss, How to create a Login with MySQL database using the CodeIgniter 3.
1. Create a controller file Signin.php in the applications/controllers/ directory.
class Signin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('Signin_model');
}
public function sign_in() {
$this->load->view(‘sign_in’);
}
public function login() {
$email = $this->input->post('email');
$password = $this->input->post('pass');
$user = $this->login_model->user_login($email, $password);
if($user) {
$this->session->set_userdata($user);
redirect(‘user/dashboard’);
} else {
echo “Invalid email address or password ”;
}
}
}2. Create a model file Signin_model.php in the applications/models/ directory.
class Signin_model extends CI_Model {
public function user_login($email, $password) {
$data = [];
$this->db->where([“email => $email, “password” => $password]);
$query = $this->db->get(‘user_info’);
$result = $query->row();
if($result) {
$data = [
‘login_id’ => $result->id,
‘login_name’ => $result->name,
‘login_email’ => $result->email,
‘login_phone’ => $result->phone,
‘login_status’ => TRUE,
];
}
return $data;
}
}3. Create a sign_in.php file in the applications/views/ directory.
<form method="post"
action="<?php echo base_url('login/login') ?>">
<table width="600" align="center" border="1"
cellspacing="5" cellpadding="5">
<tr>
<td colspan="2">
<?php echo $error; ?>
</td>
</tr>
<tr>
<td>Enter Your Email</td>
<td>
<input type="text" name="email" />
</td>
</tr>
<tr>
<td width="230">Enter Your Password</td>
<td width="329">
<input type="password" name="pass" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="login"
value="Login" />
</td>
</tr>
</table>
</form>