Membuat Login Page dengan Code Igniter Framework

Persiapan :
- Instalasi Source Code Igniter
- Instalasi Database MySQL

Membuat database untuk User  Login
Contoh database yang dibuat bernama dbpraktikum


Setelah itu buat table bernama tuser
dengan berisi field :
id type integer autoincrement sebagai primarykey,
username varchar 15
password varchar 100

Setelah selesai isi data dengan perintah berikut :
insert into tuser (username, password) values ('rangga', MD5('rahasia'));

Setelah database terbentuk maka langkah selanjutnya setting pada codeigniter code
Buka dan rubah file lokasi di : application/config/database.php
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'yourdbusername';
$db['default']['password'] = 'yourdbpassword';
$db['default']['database'] = 'yourdbname';

Setting controller default, sebagai halaman awal yang tampil saat situs anda ditampilkan. dalam hal ini saya menampilkan halaman login untuk tampil pertama kali.
untuk itu buka dan rubah file lokasi di : application/config/routes.php
$route['default_controller'] = "clogin"; 
jika secara default bawaan masih berisikan "welcome"
Berikut nya sebagai penunjang link, session kita settting pada library caranya :
Buka dan rubah file lokasi di : application/config/autoload.php
$autoload['libraries'] = array('database','session');
$autoload['helper'] = array('url'); 
untuk session nya sendiri butuh enkripsi maka dari itu buka dan rubah file lokasi di : application/config/config.php
$config['encryption_key'] = 'kode_rahasia'  
"kode_rahasia" bisa dirubah sesuai keinginan anda sebagai web developer

Setelah itu buat permodelan untuk database, buat file di:  application/models/muser.php
buka file muser.php dan isi dengan code berikut :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Muser extends CI_Model
{
 function login($username, $password)
 {
   $this -> db -> select('id, username, password');
   $this -> db -> from('tuser');
   $this -> db -> where('username', $username);
   $this -> db -> where('password', MD5($password));
   $this -> db -> limit(1);
 
   $query = $this -> db -> get();
 
   if($query -> num_rows() == 1)
   {
     return $query->result();
   }
   else
   {
     return false;
   }
 }
}
?> 
Setelah model user terbetuk maka dilanjutkan dengan membuat control login dilokasi :
application/controllers/clogin.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Clogin extends CI_Controller {

 function __construct()

 {

   parent::__construct();

 }


 function index()
 {

   $this->load->helper(array('form'));

   $this->load->view('vlogin_view');

 }

?>


Kemudian buat tampilannya di view lokasi file di application/views/login_view.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <title>Simple Login with CodeIgniter</title>
 </head>
 <body>
   <h1>Simple Login with CodeIgniter</h1>
   <?php echo validation_errors(); ?>
   <?php echo form_open('cverifylogin'); ?>
     <label for="username">Username:</label>
     <input type="text" size="20" id="username" name="username"/>
     <br/>
     <label for="password">Password:</label>
     <input type="password" size="20" id="passowrd" name="password"/>
     <br/>
     <input type="submit" value="Login"/>
   </form>
 </body>
</html>  

Untuk verifikasi dan validasi kita membuat control validator, kita buat control di application/controllers/cverifylogin.php

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

class Cverifylogin extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('muser','',TRUE);
 }

 function index()
 {
   //This method will have the credentials validation
   $this->load->library('form_validation');

   $this->form_validation->set_rules('username', 'Username', 'trim|required');
   $this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');

   if($this->form_validation->run() == FALSE)
   {
     //Field validation failed.  User redirected to login page
     $this->load->view('vlogin_view');
   }
   else
   {
     //Go to private area
     redirect('chome', 'refresh');
   }

 }

 function check_database($password)
 {
   //Field validation succeeded.  Validate against database
   $username = $this->input->post('username');

   //query the database
   $result = $this->muser->login($username, $password);

   if($result)
   {
     $sess_array = array();
     foreach($result as $row)
     {
       $sess_array = array(
         'id' => $row->id,
         'username' => $row->username
       );
       $this->session->set_userdata('logged_in', $sess_array);
     }
     return TRUE;
   }
   else
   {
     $this->form_validation->set_message('check_database', 'Invalid username or password');
     return false;
   }
 }
}
?>
Setelah itu buat cotroller home tampilan jika login berhasil
lokasi file di  application/controllers/chome.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Home extends CI_Controller {

 function __construct()
 {
   parent::__construct();
 }

 function index()
 {
   if($this->session->userdata('logged_in'))
   {
     $session_data = $this->session->userdata('logged_in');
     $data['username'] = $session_data['username'];
     $this->load->view('vhome_view', $data);
   }
   else
   {
     //If no session, redirect to login page
     redirect('clogin', 'refresh');
   }
 }

 function logout()
 {
   $this->session->unset_userdata('logged_in');
   session_destroy();
   redirect('chome', 'refresh');
 }

}

?>

Membuat tampilan setelah login berhasil di application/views/vhome_view.php
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <title>Simple Login with CodeIgniter - Private Area</title>
 </head>
 <body>
   <h1>Home</h1>
   <h2>Welcome <?php echo $username; ?>!</h2>
   <a href="chome/logout">Logout</a>
 </body>
</html>
Membuat Login Page dengan Code Igniter Framework
Item Reviewed: Membuat Login Page dengan Code Igniter Framework 9 out of 10 based on 10 ratings. 9 user reviews.
Emoticon? nyengir

Berkomentarlah dengan Bahasa yang Relevan dan Sopan.. #okemasbro! ^_^

Komentar Terbaru

Just load it!