Full Stack LAMP - MEAN Developer, Python developer. Certified Azure Developer. Freelance programmer/consultant/trainer.

In this tutorial, I will show you creating a login system using CodeIgniter. To generate decent login form design we gonna use Twitter Bootstrap styles.

We need a database and a table with user details so let’s import below sample SQL into the database or you can adjust the code according to your database, table, and data…

create DATABASE `ci_login_demo`;
  `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`)
-- Dumping data for table `users`
INSERT INTO `users` (`user_id`, `first_name`, `last_name`, `email`, `password`, `date_added`) VALUES
(1, 'Arjun', 'PHP', 'arjunphp@gmail.com', '$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.

load->helper(array('form', 'url'));

	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) {
		} 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);

			if(!password_verify($password,$user->password)) {
				$this->session->set_flashdata('login_error', 'Please check your email or password and try again.', 300);

			 $data = array(
					'user_id' => $user->user_id,
					'first_name' => $user->first_name,
					'last_name' => $user->last_name,
					'email' => $user->email,


			//redirect('/'); // redirect to home
			echo 'Login success!'; exit;

	public function logout(){

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 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'); ?>
        <button type="submit" class="btn btn-primary">Login</button>
        <?php echo $this->session->flashdata('login_error'); ?>
        <?php form_close(); ?>

Step 5: That’s it. Now head over your browser to http://localhost:3030/user/login 

      1. can you please add code for remove index.php and also while using “How to insert data into database with CodeIgniter” tutorial , I am facing issues. Is this tutorial is flexible with codeignitor 3 like latest version

Leave a Reply