Posted by Arjun on Tuesday 18th December 2018

CodeIgniter 3 - Login

Login CodeIgniter 3

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`;
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');</code>

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'	=&gt; '',
	'hostname' =&gt; 'localhost',
	'username' =&gt; 'root',
	'password' =&gt; '',
	'database' =&gt; 'ci_login_demo',
	'dbdriver' =&gt; 'mysqli',
         ....
        ......
	'save_queries' =&gt; 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.

 &#x3C;div class=&#x22;container&#x22;&#x3E;
        &#x3C;?php echo form_open(&#x27;user/login&#x27;); ?&#x3E;
        &#x3C;div class=&#x22;form-group&#x22;&#x3E;
            &#x3C;label for=&#x22;email&#x22;&#x3E;Email address&#x3C;/label&#x3E;
            &#x3C;input type=&#x22;email&#x22; class=&#x22;form-control&#x22; value=&#x22;&#x3C;?php echo set_value(&#x27;email&#x27;); ?&#x3E;&#x22; id=&#x22;email&#x22; name=&#x22;email&#x22; aria-describedby=&#x22;emailHelp&#x22; placeholder=&#x22;Enter email&#x22;&#x3E;
            &#x3C;?php echo form_error(&#x27;email&#x27;); ?&#x3E;
        &#x3C;/div&#x3E;
        &#x3C;div class=&#x22;form-group&#x22;&#x3E;
            &#x3C;label for=&#x22;password&#x22;&#x3E;Password&#x3C;/label&#x3E;
            &#x3C;input type=&#x22;password&#x22; class=&#x22;form-control&#x22; name=&#x22;password&#x22; id=&#x22;exampleInputPassword1&#x22; placeholder=&#x22;Password&#x22;&#x3E;
            &#x3C;?php echo form_error(&#x27;password&#x27;); ?&#x3E;
        &#x3C;/div&#x3E;
        &#x3C;button type=&#x22;submit&#x22; class=&#x22;btn btn-primary&#x22;&#x3E;Login&#x3C;/button&#x3E;
        &#x3C;?php echo $this-&#x3E;session-&#x3E;flashdata(&#x27;login_error&#x27;); ?&#x3E;
        &#x3C;?php form_close(); ?&#x3E;
    &#x3C;/div&#x3E;

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