Skip to main content

Codeigniter Login Form using Ajax: Codeigniter Club


Codeigniter login form with validation using Ajax

In this tutorial, we will understand how to create a simple login form using Codeigniter. 

Create database table using Mysql database

create login table in mysql database. Just copy paste query in Mysql database.

 CREATE TABLE `login` (  
 `id` int(11) NOT NULL,  
 `user_id` int(25) NOT NULL,  
 `session` varchar(10) NOT NULL,  
 `system_ip` varchar(100) NOT NULL,  
 `login_at` datetime NOT NULL DEFAULT current_timestamp(),  
 `logout_at` datetime NOT NULL  
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;  

Create view

create a file in view folder with the name of loginpage.php.

 <div class="container-scroller">  
   <div class="container-fluid page-body-wrapper full-page-wrapper">  
    <div class="content-wrapper d-flex align-items-center auth px-0">  
     <div class="row w-100 mx-0">  
      <div class="col-lg-4 mx-auto">  
       <div class="auth-form-light text-left py-5 px-4 px-sm-5">  
       <div class="brand-logo" style="text-align:center;font-weight:400;font-size:30px;">  
         <sapn style="color:orange;font-weight:600;">{...}</sapn>  
         <br>  
         <span>The Developer Club</span>  
        </div>  
        <h4>Hello! let's get started</h4>  
        <h6 class="font-weight-light">Sign in to continue.</h6>  
        <form id="login_devmediax" class="pt-3">  
         <div class="form-group">  
          <input type="email" name="email" class="form-control form-control-lg" id="exampleInputEmail1" placeholder="Email">  
         </div>  
         <div class="form-group">  
          <input type="password" name="password" class="form-control form-control-lg" id="exampleInputPassword1" placeholder="Password">  
         </div>  
         <div class="mt-3">  
          <button class="btn btn-block btn-primary btn-lg font-weight-medium auth-form-btn">SIGN IN</button>  
         </div>  
         <div class="my-2 d-flex justify-content-between align-items-center">  
          <div class="form-check">  
           <label class="form-check-label text-muted">  
            <input type="checkbox" name="remember" class="form-check-input">  
            Keep me signed in  
           </label>  
          </div>  
          <a href="#" class="auth-link text-black">Forgot password?</a>  
         </div>  
         <div class="mb-2">  
          <button type="button" class="btn btn-block btn-facebook auth-form-btn">  
           <i class="mdi mdi-facebook mr-2"></i>Connect using facebook  
          </button>  
         </div>  
         <div class="text-center mt-4 font-weight-light">  
          Don't have an account? <a href="<?php echo base_url('login/register');?>" class="text-primary">Create</a>  
         </div>  
        </form>  
       </div>  
      </div>  
     </div>  
    </div>  
    <!-- content-wrapper ends -->  
   </div>  
   <!-- page-body-wrapper ends -->  
  </div>  

Create Controller

create a controller file in controller folder with the name of Login.php. You can copy paste code in controller file. 

 public function index()  
      {  
           if($_POST){  
                $this->form_validation->set_rules('email', 'Email', 'required');  
                $this->form_validation->set_rules('password', 'Password', 'required');  
                if($this->form_validation->run() == FALSE) {  
                     //Field validation failed. User redirected to login page  
                     $this->load->view('users/login');  
                 } else {   
                     $sessArray = array();  
                     //Field validation succeeded. Validate against database  
                     $email = $this->input->post('email');  
                     $password = $this->input->post('password');  
                     $data = array('email'=>$email,  
                                         'password'=>MD5($password));  
                     //query the database  
                     $result = $this->Login_Model->check_login($data);  
                     if($result) {  
                      foreach($result as $row) {  
                          $sessArray = array(  
                           'user_id' => $row->username,  
                           'email' => $row->email,  
                           'password' => $row->password,  
                           'is_authenticated' => TRUE,  
                          );  
                      $this->session->set_userdata($sessArray);  
                      }  
                      $this->load->view('include/header');  
                      $this->load->view('admin/dashboard');  
                      $this->load->view('include/footer');  
                      redirect('home',$sessArray);  
                     } else {  
                      redirect('login?msg=1');  
                     }   
                 }  
           }else{  
                $this->load->view('login');  
           }  
      }  

Create Model

create a model file in model folder with the name of Login_Model.php. You can copy paste code in model file. 

 public function check_login($data){  
     $this -> db -> select('username, email, password');  
     $this -> db -> from('registration');  
     $this -> db -> where('email', $data['email']);  
     $this -> db -> where('password', $data['password']);
     $query = $this -> db -> get();  
     if($query -> num_rows() != 0) {  
     return $query->result();  
     } else {  
     return false;  
     }  
   }  


Useful Link

How to secure codeigniter website?

Codeigniter registration form with validation


Comments

Popular posts from this blog

Codeigniter installation & setup: Codeigniter Club

                    Windows Environment   Install XAMPP or WAMP   Download and Unzip the package from Codeigniter.com   Extract all the documents in the server space (htdocs or www directory) Linux Environment  Download and Unzip the package from Codeigniter.com  Place the extracted folder in /var/www (in WAMP) or xampp/htdocs (XAMPP) Base URL   Go to application/config/config.php  Define base URL as $config['base_url'] = 'http://localhost/path/to/folder'; How to remove index.php from URL? go to root  create htaccess file  Add below code inside it  RewriteEngine on RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA] Note: .htaccess code varies depending on the hosting server. Some hosting servers (e.g.: Godaddy) need to use an extr...

What is Codeigniter?

Welcome to Codeigniter Codeigniter is an Application development framework. Codeigniter framework is based on the Model-View-Controller development pattern. MVC is a software approach that separates application logic from presentation. Current Version Codeigniter 4 Old Version Codeigniter 3 Model Model is used to access data from the database. If we need to get data from the database so we have to create a query (logic) for the database. View The view is used to create a view of the website. We can say, design of the website. We can create view (HTML, CSS, JavaScript, Ajax etc.). Users directly interact with the view of the website. Controller A controller is responsible for controlling the way that users interact with the MVC application. A controller is used to create logic for an application.  

Stripe payment gateway integration using cUrl - Codeigniter 3

  Stripe payment gateway integration using cUrl - Codeigniter 3 Sign up for a Stripe account at https://stripe.com/ Obtain Stripe api key Setup Codeigniter 3 Project Create a library for Stripe integration Create library inside libraries folder   application => libraries => Stripe_library.php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Stripe_library { protected $CI; // Default Constructor public function __construct() { $this->CI =& get_instance(); // Load necessary libraries and helpers here if needed } // Create function for checkout public function create_checkout_session($amount, $currency, $success_url, $cancel_url) { $stripe_secret_key = 'Stripe_secret_key'; $api_url = 'https://api.stripe.com/v1/checkout/sessions'; $stripeamount = $amount * 100; $data = [ 'payment_method_types' => ['card'], ...