Last updated on November 17, 2022
In this tutorial, I will show you creating a login system using CodeIgniter. To generate a decent login form design we gonna use Twitter Bootstrap styles.
We need a database and a table with user details so let’s import the below sample SQL into the database or you can adjust the code according to your database, table, and data…
create DATABASE `ci_login_demo`;
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`user_id`, `first_name`, `last_name`, `email`, `password`, `date_added`) VALUES
(1, 'Arjun', 'PHP', '[email protected]', '$2y$10$8mVSGv/bIGgcvCikXBPfTu7HfXMl3jqfiirtQGyRwV5bvOzNGmmLG', '2018-12-17 18:09:10');
Here are the steps to build a simple login system using CodeIgniter 3
Step 1: Download and install CodeIgniter.
Step 2: Open application/config/config.php
file and set your base URL, mine is $config['base_url'] = 'http://localhost:3030/';
and update
sess_save_path to $config['sess_save_path'] = sys_get_temp_dir();
Step 3: Open application/config/database.php
file and set your database config details, here is mine
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'ci_login_demo',
'dbdriver' => 'mysqli',
....
......
'save_queries' => TRUE
);
Step 3: Now create a controller file called User.php
in application/controllers
, with blow login and logout methods.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library(['form_validation','session']);
$this->load->database();
}
public function login() {
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('login_view');
} else {
$email = $this->input->post('email');
$password = $this->input->post('password');
$user = $this->db->get_where('users',['email' => $email])->row();
if(!$user) {
$this->session->set_flashdata('login_error', 'Please check your email or password and try again.', 300);
redirect(uri_string());
}
if(!password_verify($password,$user->password)) {
$this->session->set_flashdata('login_error', 'Please check your email or password and try again.', 300);
redirect(uri_string());
}
$data = array(
'user_id' => $user->user_id,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
);
$this->session->set_userdata($data);
//redirect('/'); // redirect to home
echo 'Login success!'; exit;
}
}
public function logout(){
$this->session->sess_destroy();
redirect('user/login');
}
}
Step 4: Now create a login form view file called login_form.php
in application/views
directory, along with blow Html code.
<div class="container">
<?php echo form_open('user/login'); ?>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" value="<?php echo set_value('email'); ?>" id="email" name="email" aria-describedby="emailHelp" placeholder="Enter email">
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" id="exampleInputPassword1" placeholder="Password">
<?php echo form_error('password'); ?>
</div>
<button type="submit" class="btn btn-primary">Login</button>
<?php echo $this->session->flashdata('login_error'); ?>
<?php form_close(); ?>
</div>
Step 5: That’s it. Now head over to your browser and visit http://localhost:3030/user/login