This is default featured post 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

Showing posts with label login. Show all posts
Showing posts with label login. Show all posts

Friday, January 28, 2011

Create Authentication Login On Code Igniter With Extends Controller

  1. Create MY_Controller.php on /system/apllication/libraries/.
  2. <?php
    class MY_Controller extends Controller {
    
        function  My_Controller() {
            parent::Controller();
            $this->is_logged_in();
        }
    
        private function is_logged_in() {
            $this->ci =& get_instance();
            $this->ci->load->library('session');
            if(! $this->ci->session->userdata('login')) {
                redirect('login');
            }
        }
    
    }
    ?>
  3. Login Controller login.php
  4. <?
    
    /**
     * Login Class
     *
     *
     */
    class Login extends Controller {
     /**
      * Constructor
      */
     function login()
     {
      parent::Controller();
      $this->load->model('Login_model', '', TRUE);
     }
    
     /**
      * Check User Status, if login status it will appear in main page,
      * If Not it'll redirect in login page
      */
     function index()
     {
      if ($this->session->userdata('login') == TRUE)
      {
       redirect('home');
      }
      else
      {
       $this->load->view('login/login_view');
      }
     }
    
     /**
      * Login
      */
     function process_login()
     {
    
      $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');
    
       if ($this->Login_model->check_user($username, $password) == TRUE)
       {
        $row = $this->Login_model->data_user($username);
        $data = array('jbname' => $username, 'login' => TRUE, 'jbnama' => $row['nama'], 'jbid' => $row['id_user']);
        $this->session->set_userdata($data);
        redirect('home/');
       }
       else
       {
        $this->session->set_flashdata('message', 'Sorry, username and password is wrong');
        redirect('login/index');
       }
      }
      else
      {
       $this->load->view('login/login_view');
      }
     }
    
     /**
      * logout
      */
     function process_logout()
     {
      $this->session->sess_destroy();
      redirect('login', 'refresh');
     }
    }
    
  5. Make Model Login_model.php
  6. <?php
    /**
     * Login_model Class
     *
     *
     */
    class Login_model extends Model {
     /**
      * Constructor
      */
     function Login_model()
     {
      parent::Model();
     }
    
     // Inisialisation user table name
     var $table = 'user';
    
     /**
      * Check user table
      */
     function check_user($username, $password)
     {
      $password = md5($password);
      $query = $this->db->get_where($this->table, array('username' => $username, 'pass' => $password, 'aktif' => 1,
      'nm_module' => 'akd'), 1, 0);
    
      if ($query->num_rows() > 0)
      {
       return TRUE;
      }
      else
      {
       return FALSE;
      }
     }
    
     function data_user($username){
      $sql = "select id_user, nama from user where username = '$username' and nm_module = 'akd' ";
      $query = $this->db->query($sql);
      $row = $query->row_array();
      return $row;
     }
    
    }
    // END Login_model Class
    
    /* End of file login_model.php */
    /* Location: ./system/application/model/login_model.php */