Logo

Office Address

123/A, Miranda City Likaoli
Prikano, Dope

Phone Number

+0989 7876 9865 9

+(090) 8765 86543 85

Email Address

info@example.com

example.mail@hum.com

Login Form in CodeIgniter

Login Form in CodeIgniter

In this article, we are going to explain how to make a simple login form with sessions and Login in the Codeigniter framework. In this tutorial, we will make simple login forms with sessions by using the Codeigniter framework. In the Codeigniter framework creating sessions is different than simple PHP. We will discuss how to create sessions for login into the system in the Codeigniter framework. Here we will discuss how can we use the model view controller coding style to validate user information and if the user has entered the right login information and then create a simple session variable in the Codeigniter framework. Here the system is working on model view controller coding style, that means here system has received login data from the view and from view it has been sending to the controller and then after the controller has sent that data to models for validating data are proper or not, then after model send back the result to controller and controller send back result to view. This way the login system is working in the Codeigniter framework. This is my simple CodeIgniter application for the login system.

autoload.php

 $autoload['libraries'] = array('database', 'session');   
 //For Load Session Library in this application  
 ?>  

config.php

   $config['encryption_key'] = 'xRUqKhsoZ5qV6y3kqARFJFdPqJvp7X2z';  
 // For encrypt session data by using encryption class  
 ?>  

Controllers - main.php

   defined('BASEPATH') OR exit('No direct script access allowed');  
 class Main extends CI_Controller {  
      //functions  
      function login()  
      {  
           //http://localhost/blazingcoders/codeigniter/main/login  
           $data['title'] = 'CodeIgniter Simple Login Form With Sessions';  
           $this->load->view("login", $data);  
      }  
      function login_validation()  
      {  
           $this->load->library('form_validation');  
           $this->form_validation->set_rules('username', 'Username', 'required');  
           $this->form_validation->set_rules('password', 'Password', 'required');  
           if($this->form_validation->run())  
           {  
                //true  
                $username = $this->input->post('username');  
                $password = $this->input->post('password');  
                //model function  
                $this->load->model('main_model');  
                if($this->main_model->can_login($username, $password))  
                {  
                     $session_data = array(  
                          'username'     =>     $username  
                     );  
                     $this->session->set_userdata($session_data);  
                     redirect(base_url() . 'main/enter');  
                }  
                else  
                {  
                     $this->session->set_flashdata('error', 'Invalid Username and Password');  
                     redirect(base_url() . 'main/login');  
                }  
           }  
           else  
           {  
                //false  
                $this->login();  
           }  
      }  
      function enter(){  
           if($this->session->userdata('username') != '')  
           {  
                echo '

Welcome - '.$this->session->userdata('username').'

';  
                echo 'Logout';  
           }  
           else  
           {  
                redirect(base_url() . 'main/login');  
           }  
      }  
      function logout()  
      {  
           $this->session->unset_userdata('username');  
           redirect(base_url() . 'main/login');  
      }  
 }  

 

Models - main_model.php

   class Main_model extends CI_Model  
 {  
      function can_login($username, $password)  
      {  
           $this->db->where('username', $username);  
           $this->db->where('password', $password);  
           $query = $this->db->get('users');  
           //SELECT * FROM users WHERE username = '$username' AND password = '$password'  
           if($query->num_rows() > 0)  
           {  
                return true;  
           }  
           else  
           {  
                return false;       
           }  
      }  
 }  

Views - login.php

   
 

 
 
 
     Blazingcoders  
       
   
 

 
           


 
           
 
               
 
                     Enter Username  
                       
                     

 

 

>                 
               

 
               
 
                     Enter Password  
                       
                     

 

 

>  
               

 
               
 
                       
                                                echo ''.$this->session->flashdata

 

 

("error").'';  
                     ?>  
               

 
            

 

 

Tags

  • Codeigniter tutorial

Related Posts

Custom T-Shirt ECommerce Website Development Company

Custom T-Shirt ECommerce Website Development Company

In the digital era, eCommerce has become the cornerstone of retail success. Businesses across various sectors are moving online to meet the growing demand for convenience and personalization. Among th

Read More
Upload CSV files and import to Database using Laravel PHP

Upload CSV files and import to Database using Laravel PHP

In this article, we are how to upload CSV file from a system and import in the database using Laravel. There are countless bundles accessible in Laravel to import dominant documents in the data set

Read More
When Do You Need a Custom Post Type or Taxonomy in WordPress?

When Do You Need a Custom Post Type or Taxonomy in WordPress?

Post Type and Custom Post type are used to post Content in Wordpress. its knows as Content Type. There are some post type already exit in wordpress attachments, pages, revisions and navigation menus.

Read More