Create login and registration in codeigniter and ci

Create Login and Registration in Codeigniter,Create Login and Registration in ci

configuration of ci, create login and registration in ci, basic installation of ci, create session in ci, check session in Codeigniter,load labaries,load helper,use helper in ci, url helper, create anchor tag in ci, model, controller, structure of ci, working on ci.

Configuration of Ci

Controller

Go to application->controllers->UsersController.php and insert the following code

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class UsersController extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *         http://example.com/index.php/welcome
     *    - or -  
     *         http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
     function __construct()
      {
        parent::__construct();
        $this->load->model('Users');
    }


    public function index()
    { 
        if(is_logged_in()){
            redirect('UsersController/dashboard', 'refresh');
        }
        else
        {
            $data = $this->Users->validate();
            if ($data)
             {
            $this->session->set_userdata('user_data', $data);
            redirect('UsersController/dashboard', 'refresh');
            } 
            else
             {
                $this->session->set_flashdata('message', 'Invalid username or password');
            }
            
        $this->load->view('Users/login');
        }    
        
        
    }

}
?>

Create View

Now go to application\views create folder Users and create file name login.php and insert the following code

<h3 align="center">Login</h3>

<table width="600" align="center" style="border:solid 2px #CCCCCC;">
<?php echo form_open("UsersController/index"); ?>
  <tr>
    <td colspan="2">
    <?php $msg = $this->session->flashdata('message');
        if(isset($msg)){
        echo $msg;
        } ?>
    </td>
   
  </tr>
  <tr>
    <td>Email</td>
    <td><?php $data = array('name' => 'email', 'id' => 'email', 'placeholder' => 'Email');
        echo form_input($data); ?>
    </td>
  </tr>
  <tr>
    <td>Password</td>
    <td>
    <?php echo form_password(array('name' => 'pass', 'id' => 'pass', 'placeholder' => 'Password'));?>
 </td>
  </tr>
  <tr>
    <td><button name="submit" onClick="document.form_name.submit()">Sign In</button></td>
    <td><?php echo anchor('UsersController/register','Register');?></td>
  </tr>
  <?php
echo form_close(); ?>
</table>
first

Model

Now go to application->models->Users.php

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Users extends CI_Model {

function __construct() {
        parent::__construct();
        $this->load->database();
        
    }

    function validate() {
    
    //print_r($_POST);die;
        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password', base64_encode($this->input->post('pass')));
        $query = $this->db->get_where('users');
        if ($query->num_rows == 1) 
        {
            $row = $query->row_array(); 
                return $row;
        }
        else
        {
                return false;
        }
    }
?>

 

registration function

open your UsersController.php and replace the code form these

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class UsersController extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *         http://example.com/index.php/welcome
     *    - or -  
     *         http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
     function __construct()
      {
        parent::__construct();
        $this->load->model('Users');
    }


    public function index()
    { 
        if(is_logged_in()){
            redirect('UsersController/dashboard', 'refresh');
        }
        else
        {
            $data = $this->Users->validate();
            if ($data)
             {
            $this->session->set_userdata('user_data', $data);
            redirect('UsersController/dashboard', 'refresh');
            } 
            else
             {
                $this->session->set_flashdata('message', 'Invalid username or password');
            }
            
        $this->load->view('Users/login');
        }    
        
        
    }


    
    public function register()
    {
        if(isset($_POST['submit']))
         {
             if($this->input->post('pass')!==$this->input->post('cpass'))
            {
            $this->session->set_flashdata('message', 'Password and Confirm Password not Match');
            $this->load->view('Users/register');
            }else{
            
            $data = array(
                'f_name' => $this->input->post('fname'),
                'l_name' => $this->input->post('lname'),
                'email' => $this->input->post('email'),
                'password' => base64_encode($this->input->post('pass')),
                    );
                 $this->Users->register($data);
                 $this->session->set_flashdata('message', 'Register Successfully.');
                 $this->load->view('Users/register');
            }
        
        }else{
        $this->load->view('Users/register');
        }
    }
}    
?>

 

Add new function in Model/Users.php

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Users extends CI_Model {

function __construct() {
        parent::__construct();
        $this->load->database();
        
    }

    function validate() {
    
    //print_r($_POST);die;
        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password', base64_encode($this->input->post('pass')));
        $query = $this->db->get_where('users');
        if ($query->num_rows == 1) 
        {
            $row = $query->row_array(); 
                return $row;
        }
        else
        {
                return false;
        }
    }
    
    public function register($data)
    {
    $this->db->insert('users', $data);
    }
    
}
?>

 

Create registration View file

Now go to application->view->Users and create file register.php and insert the following code

<h3>Register</h3>


<table width="600" border="1">
<?php echo form_open("UsersController/register");?>
  <tr>
    <td colspan="2">
        <?php 
        $msg = $this->session->flashdata('message');
        if(isset($msg)){
        echo $msg;
        }?>
    </td>
   
  </tr>
  <tr>
    <td>First Name</td>
    <td>
    <?php 
    $data = array('name' => 'fname', 'id' => 'fname', 'placeholder' => 'First Name');
    echo form_input($data);
    ?>
    </td>
  </tr>
  <tr>
    <td>Last Name</td>
    <td>
    <?php 
    $data = array('name' => 'lname', 'id' => 'LastName', 'placeholder' => 'Last Name');
    echo form_input($data);
    ?>
    
    </td>
  </tr>
  <tr>
    <td>Email</td>
    <td>
    <?php 
    $data = array('name' => 'email', 'id' => 'email', 'placeholder' => 'Email');
    echo form_input($data);
    ?>
    </td>
  </tr>
  <tr>
    <td>Password</td>
    <td><?php echo form_password(array('name' => 'pass', 'id' => 'pass', 'placeholder' => 'Password')); ?></td>
  </tr>
  <tr>
    <td>Confirm Password</td>
    <td><?php echo form_password(array('name' => 'cpass', 'id' => 'cpass', 'placeholder' => 'Confirm Password')); ?></td>
  </tr>
   <tr>
    <td><button name="submit" onClick="document.form_name.submit()">Register</button></td>
    <td><?php echo anchor('UsersController/index','Login');?></td>
  </tr>
  
  <?php 
echo form_close(); ?>
</table>
second

Create dashboard View file

Now go to application->view->Users and create file name dashboard.php

<p><?php echo anchor('UsersController/logout','Logout');?></p>
<?php 
echo "<pre>";
print_r($this->session->all_userdata());
?>
3

Add logout function in UsersController.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class UsersController extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *         http://example.com/index.php/welcome
     *    - or -  
     *         http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
     function __construct()
      {
        parent::__construct();
        $this->load->model('Users');
    }


    public function index()
    { 
        if(is_logged_in()){
            redirect('UsersController/dashboard', 'refresh');
        }
        else
        {
            $data = $this->Users->validate();
            if ($data)
             {
            $this->session->set_userdata('user_data', $data);
            redirect('UsersController/dashboard', 'refresh');
            } 
            else
             {
                $this->session->set_flashdata('message', 'Invalid username or password');
            }
            
        $this->load->view('Users/login');
        }    
        
        
    }


    
    public function register()
    {
        if(isset($_POST['submit']))
         {
             if($this->input->post('pass')!==$this->input->post('cpass'))
            {
            $this->session->set_flashdata('message', 'Password and Confirm Password not Match');
            $this->load->view('Users/register');
            }else{
            
            $data = array(
                'f_name' => $this->input->post('fname'),
                'l_name' => $this->input->post('lname'),
                'email' => $this->input->post('email'),
                'password' => base64_encode($this->input->post('pass')),
                    );
                 $this->Users->register($data);
                 $this->session->set_flashdata('message', 'Register Successfully.');
                 $this->load->view('Users/register');
            }
        
        }else{
        $this->load->view('Users/register');
        }
    }
    
    
    
    public function dashboard()
    {
        $this->load->view('Users/dashboard');
    }
    
     
     public function logout(){
        $this->session->unset_userdata('user_data');
        $this->session->set_flashdata('message', 'you have successfully logout');
         redirect('UsersController/index', 'refresh');
    }
    
}

Its the complete code of UsersController.php


Complete Model Users.php

 

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Users extends CI_Model {

function __construct() {
        parent::__construct();
        $this->load->database();
        
    }

    function validate() {
    
    //print_r($_POST);die;
        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password', base64_encode($this->input->post('pass')));
        $query = $this->db->get_where('users');
        if ($query->num_rows == 1) 
        {
            $row = $query->row_array(); 
                return $row;
        }
        else
        {
                return false;
        }
    }
    
    public function register($data)
    {
    $this->db->insert('users', $data);
    }
    
    
    
}
Share Button

About admin

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Author: pramod kumar kumawat Mob: 9269727062
Free WordPress Themes - Download High-quality Templates