Membuat Login Menggunakan CodeIgneter

1.  Buat database dengan nama “crud_sqli”
2. Selanjutnya membat table dengan nama “ admin “

3. Setelah itu buka codeigneternya masuk ke folder application pilih “config -> database “.
Masukan nama database yang kamu buat contohnya “ crud_sqli


4. Kemudian ke file routes
Buat default_controllernya dengan nama file “login”

5.Setelah semua sudah di setting di folder config selanjutnya kita akan membuat control loginnya

6. Buka file controller, buat file baru dengan nama “ login” .Silahkan copy paste coding di bawah ini untuk controller login

<?php

class Login extends CI_Controller{

                function __construct(){
                                parent::__construct();                  
                                $this->load->model('m_login');

                }

                function index(){
                                $this->load->view('v_login');
                }

                function aksi_login(){
                                $username = $this->input->post('username');
                                $password = $this->input->post('password');
                                $where = array(
                                                'username' => $username,
                                                'password' => md5($password)
                                                );
                                $cek = $this->m_login->cek_login("admin",$where)->num_rows();
                                if($cek > 0){

                                                $data_session = array(
                                                                'nama' => $username,
                                                                'status' => "login"
                                                                );

                                                $this->session->set_userdata($data_session);

                                                redirect(base_url("index.php/admin"));

                                }else{
                                                echo "Username dan password salah !";
                                }
                }
  // untuk menampilkan halaman register
 
  //menampilkan data
               
  
  public function register()
  {
      return $this->load->view('v_register');
  }
 
  //action untuk simpan data
                function perintah_tambah(){
                                $username = $this->input->post('username');
                                $password = md5($this->input->post('password'));
                $nama = $this->input->post('nama');
                $jk = $this->input->post('jk');
                $email = $this->input->post('email');
                $telepon = $this->input->post('telepon');
               
                                $data = array(
                                               
                                                'username' => $username,
                                                'password' => $password,
                                                'nama' => $nama,
                                                'jk' => $jk,
                                                'email' => $email,
                                                'telepon' => $telepon
                                               
                                                );
                                $this->m_login->input_data($data,'admin');
                                redirect('login/index');
                }
               
 
 
                function logout(){
                                $this->session->sess_destroy();
                                redirect(base_url('index.php/login'));
                }
}

7. Selanjutnya kita beralih ke folder models yang ada di folder application. Buat file baru dengan nama “M_login”



<?php

class M_login extends CI_Model{             
                function cek_login($table,$where){                        
                                return $this->db->get_where($table,$where);
                }             
                //simpan data
                function input_data($data,$table){
                                $this->db->insert($table,$data);
                }
}

8. Selanjutnya kita ke volder view. Buat file baru dengan nama “ v_login .php“


<!DOCTYPE html>
<html>
<head>
                <title></title>
</head>
<body>
                <h1>&nbsp;</h1>
                <form action="<?php echo base_url('index.php/login/aksi_login'); ?>" method="post">                 
                                <table>
                                                <tr>
                                                                <td>Username</td>
                                                                <td><input type="text" name="username"></td>
                                                </tr>
                                                <tr>
                                                                <td>Password</td>
                                                                <td><input type="password" name="password"></td>
                                                </tr>
                                                <tr>
                                                                <td></td>
                                                                <td><input type="submit" value="Login"></td>
                                                </tr>
                                </table>
                </form>
                 <hr>
      <a href="<?=base_url()?>index.php/login/register" class="btn btn-success">REGISTER</a>
</body>
</html>




1.      Jangan lupa ya untuk extensen file nya .php
Semoga bias membantu teman – teman dalam menyelesaikan tugasnya, jika ada yang masih error silahkan komen ya. Pasti saya akan membantu teman – teman semua.


TAMPILAN







0 komentar:

Posting Komentar