Membuat Form Login Dengan CodeIgniter

Jaka Senin, 20 Maret 2017 14:48:36 Web Development 3986

Setelah berhasil mengintegrasikan CodeIgniter dan Template Admin Bootstrap, selanjutnya pada postingan kali ini saya akan memberikan tutorial Membuat Form Login Dengan CodeIgniter. Agar lebih jelas dan berhasil mencoba contoh tutorial ini sebaiknya baca dan ikuti postingan sebelumnya mengenai Cara Menghilangkan index.php dan Pengaturan Dasar Pada CodeIgniter dan Integrasi CodeIgniter dan Template Admin Bootstrap. Karena pada contoh ini saya akan menggunakan Template SB admin Bootstrap dan pada postingan ini saya tidak menjelaskan lagi langkah - langkah settingan awal CodeIgniter dan mengintegrasikan CodeIgniter dan Template Admin BootstrapOk, langsung saja, berikut langkah - langkahnya :

1. Buat database MySQL (contoh : db_latihan), dan tabel tbl_user dengan struktur tabel sebagai berikut :

--
-- Table structure for table `tbl_user`
--

CREATE TABLE IF NOT EXISTS `tbl_user` (
`nama_user` varchar(50) COLLATE latin1_general_ci NOT NULL,
`password` varchar(50) COLLATE latin1_general_ci NOT NULL,
`nama_lengkap` varchar(100) COLLATE latin1_general_ci NOT NULL,
PRIMARY KEY (`nama_user`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

--
-- Dumping data for table `tbl_user`
--

INSERT INTO `tbl_user` (`nama_user`, `password`, `nama_lengkap`) VALUES
('admin', '21232f297a57a5a743894a0e4a801fc3', 'Jaka Kharisma Dhona');

2. Setting koneksi database, buka file database.php yang berada di dalam folder config, ubah pada bagian berikut :

$db['default']['hostname'] = 'localhost'; 
$db['default']['username'] = 'root'; //diisi sesuai nama user mysql
$db['default']['password'] = ''; //diisi sesuai password mysql
$db['default']['database'] = 'db_latihan'; //diisi sesuai nama database mysql
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

3. Buka file config.php yang berada di dalam folder config, ubah pada bagian $config['base_url'] sebagai berikut :

$config['base_url'] = 'http://localhost/latihan/';  //diisi sesuai url utama / domain yang kita buat

dan $config['encryption_key'] ubah sebagai berikut :

$config['encryption_key'] = 'alkarizma'; // bisa diisi sesuai keinginan

4. Selanjutnya buka file config.php yang berada di dalam folder config, ubah pada bagian $autoload['libraries'] sebagai berikut :

$autoload['libraries'] = array('database','session','form_validation');

5. Karena halaman awal yang akan diload adalah halaman login, jadi perlu kita rubah dahulu settingan default_controller pada file routes.php yang berada di dalam folder config sebagai berikut :

$route['default_controller'] = "login";

6. Sesuai dengan konsep MVC (Model, View,  Controller), langkah pertama yang kita buat adalah file model yang berisi function query data untuk cek login user. Buat file m_user di dalam folder models, ketikkan kode sebagai berikut :

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

class M_user extends CI_Model {

function cek($nama_user, $password) {
$this->db->where("nama_user", $nama_user);
$this->db->where("password", $password);
return $this->db->get("tbl_user");
}

}

7. Setelah itu kita buat view desain form login, tapi sebelumnya karena pada Template SB admin Bootstrap tidak ada desain form login, kama kita perlu membuat desain css untuk form login. buat file login.css di dalam folder assets/css (baca : Integrasi CodeIgniter dan Template Admin Bootstrap), ketikkan script css sebagai berikut :

/** Login Page **/
#login {
padding-top: 40px;
padding-bottom: 40px;
}

#login .form-signin {
max-width: 300px;
padding: 19px 29px 29px;
margin: 0 auto 20px;
background-color: #fff;
border: 1px solid #e5e5e5;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
box-shadow: 0 1px 2px rgba(0,0,0,.05);
}
#login .form-signin .form-signin-heading,
#login .form-signin .checkbox {
margin-bottom: 10px;
}
#login .form-signin input[type="text"],
#login .form-signin input[type="password"] {
font-size: 16px;
height: auto;
margin-bottom: 15px;
padding: 7px 9px;
}

8. Berikutnya kita buat desain view form login, buat file login.php di dalam folder views/admin (baca : Integrasi CodeIgniter dan Template Admin Bootstrap), ketikkan kode sebagai berikut :

<!DOCTYPE html>
<html>
<head>
<title>Admin Login</title>
<!-- Bootstrap -->
<link href="<?php base_url() ?>assets/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="<?php base_url() ?>assets/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
<link href="<?php base_url() ?>assets/css/login.css" rel="stylesheet" media="screen">
</head>
<body id="login">
<div class="container">
<form class="form-signin" action="<?php echo base_url('login/proses'); ?>" method="post">
<h2 class="form-signin-heading">Silahkan Login</h2>
<?php
if (validation_errors() || $this->session->flashdata('result_login')) {
?>
<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">&times;</button>
<strong>Warning!</strong>
<?php echo validation_errors(); ?>
<?php echo $this->session->flashdata('result_login'); ?>
</div>
<?php } ?>
<input type="text" class="input-block-level" name="namauser" placeholder="Nama User">
<input type="password" class="input-block-level" name="password" placeholder="Password">
<button class="btn btn-primary" type="submit">Login</button>
</form>

</div> <!-- /container -->
</body>
</html>

9. Selanjutnya kita membuat file di dalam folder controllers dengan nama login.php lalu ketikkan code sebagai berikut :

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

function __construct() {
parent::__construct();
session_start();
$this->load->model('m_user');
}

function index()
{
$this->load->view('admin/login');
}

function proses() {
$this->form_validation->set_rules('namauser', 'namauser', 'required|trim|xss_clean');
$this->form_validation->set_rules('password', 'password', 'required|trim|xss_clean');

if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('result_login', '<br>Nama atau Password belum diisi.');
redirect();
} else {
$tgl_login=date("Y-m-d");
$jam_login=date("H:i:s");
$usr = $this->input->post('namauser');
$psw = $this->input->post('password');
$u = mysql_real_escape_string($usr);
$p = md5(mysql_real_escape_string($psw));
$cek = $this->m_user->cek($u, $p);
if ($cek->num_rows() > 0) {
//login berhasil, buat session
foreach ($cek->result() as $qad) {
$sess_data['nama'] = $qad->nama_lengkap;
$sess_data['u_name'] = $qad->nama_user;
$sess_data['pwd'] = $qad->password;
$sess_data['tgl_login'] = $tgl_login;
$sess_data['jam_login'] = $jam_login;
$this->session->set_userdata($sess_data);
}
redirect('dashboard');
} else {
$this->session->set_flashdata('result_login', '<br>Username atau Password yang anda masukkan salah.');
redirect();
}
}
}

function logout() {
$this->session->sess_destroy();
redirect('login');
}

}

Sekarang bisa kita coba test login, buka browser, ketikkan url localhost/latihan, lalu coba masukkan nama user : admin dan pasword : admin. Ketika login berhasil maka kita akan di arahkan ke halaman dashboard yang pernah kita buat sebelumnya pada postingan Integrasi CodeIgniter dan Template Admin Bootstrap.

Ok, sekian tutorial Membuat Form Login Dengan CodeIgniter ini, bila teman - teman ingin download script lengkapnya silahkan download di link berikut.

Artikel Terkait :

Integrasi CodeIgniter dan Template Admin Bootstrap

8 Fitur Rahasia Android Yang Banyak Tidak Diketahui Penggunanya

Microsoft Akan Menghentikan Dukungannya Terhadap Sistem Operasi Windows Vista

Template Admin & Dashboard Pilihan Alkarizma.com

Perbedaan Include dan Require Pada PHP


Komentar :



55 Komentar :

Uxivi65

22 Oktober 2017 - 18:34:56

http://www.ocperio.org/group3/blogs/post/13061\nhttp://www.tennis-motion-connect.com/blogs/post/11573\nhttp://saknada.com.au/ssn/blogs/post/80573\nhttp://www.sbschina.ru/?option=com_k2&view=itemlist&task=user&id=14024\nhttp://lesko.com/q2a/index.php?qa=1156&qa_1=salbutamol-ligne-marche-securise-acheter-ventolin-belgique\nhttp://diduknow.allrh.com/blogs/post/12931\nhttp://www.bloggerfree.com/blogs/post/81505\nhttp://ihumanmgt.org/blogs/post/3124\nhttp://komunitas.emerer.com/blogs/post/2088\nhttp://amusecandy.com/blogs/post/81605\nhttp://lifestir.net/blogs/post/17397\nhttp://support.myyna.com/38244/farmacia-online-comprar-moxifloxacin-receta-comprar-avelox\nhttp://www.mauiwowie.xxx/social/blogs/post/2597\nhttp://www.askongole.com/v1/q2a/index.php?qa=277&qa_1=farmacia-online-comprar-dutasterida-receta-rĂ¡pido-argentina\nhttp://saknada.com.au/ssn/blogs/post/40611\nhttp://www.haiwaishijie.com/547/buy-telmisartan-20-mg-on-sale-how-to-buy-micardis-without-rx\nhttp://www.nostre.com/blogs/post/13458\nhttp://www.albaservicemarmi.it/?option=com_k2&view=itemlist&task=user&id=47810\nhttp://www.tennis-motion-connect.com/blogs/post/53032\nhttp://www.q-voice.tv/old/redirect/test/qa/index.php?qa=3113&qa_1=farmacia-comprar-generico-venlafaxine-receta-garantia-ecuador\nhttp://saknada.com.au/ssn/blogs/post/25310\nhttp://qna.kansagara.in//index.php?qa=100&qa_1=buy-sevelamer-800mg-how-to-buy-renagel-online\nhttp://dmoney.ru/2397/thioridazine-25mg-order-online-mellaril-guaranteed-delivery\nhttp://www.informatique.pro/8317/salbutamol-4-mg-acheter-2017-achat-ventolin-rapide\nhttps://www.olliesmusic.com/blog/28020/purchase-low-price-lamivudine-150mg-where-to-buy-epivir-safely/\nhttp://www.tennis-motion-connect.com/blogs/post/54971\nhttp://88.88maw.com/blogs/post/56730\nhttp://www.littleblackdress.al/qtoa/index.php?qa=1615&qa_1=avanafilo-comprar-internet-comprar-stendra-original-online\nhttp://southweddingdreams.com/index.php?do=/blog/72157/order-cheap-sildenafil-citrate-100-mg-on-sale/\nhttp://lifestir.net/blogs/post/45925\nhttp://southweddingdreams.com/index.php?do=/blog/72421/cyproheptadine-buy/\nhttp://myturnondemand.com/oxwall/blogs/post/237794\n \nkxonrf8jrlu4

LarryWhilt

22 Oktober 2017 - 14:28:41

http://news-fakti.com/component/k2/itemlist/user/10195 ?????? ???? ???????? ? ??????? ???????? ????????? ???????? ?????????? ?????? ?????? 2014 ???? http://studiomartiniroberta.it/index.php?option=com_k2&view=itemlist&task=user&id=7345

Oduci94

22 Oktober 2017 - 10:51:08

minipress costs generic, http://www.printjunkie.net/profiles/blogs/minipress-prazosin-1-mg-comprar-sin-receta-fiable-espa-a, http://www.godry.co.uk/profiles/blogs/achat-minipress-prazosin-2mg-minipress-homme-achat http://www.godry.co.uk/profiles/blogs/achat-vente-minipress-5mg-bon-marche-paiement-visa-avis-acheter, http://www.godry.co.uk/profiles/blogs/achat-vente-minipress-5mg-bon-marche-paiement-visa-avis-acheter http://opencu.com/profiles/blogs/minipress-5-mg-puedo-comprar-barato-panam http://ssbsavannah.ning.com/profiles/blogs/minipress-prazosin-moins-cher-sans-ordonnance-prazosin-5, http://neolatino.ning.com/profiles/blogs/minipress-acheter-2017-achat-minipress-paris, generic for minipress what is cost http://www.godry.co.uk/profiles/blogs/achat-minipress-prazosin-2mg-minipress-homme-achat

Popaw94

22 Oktober 2017 - 07:57:25

http://saknada.com.au/ssn/blogs/post/15676\nhttps://www.olliesmusic.com/blog/32793/farmacia-online-donde-comprar-generico-loxapine-10-mg-sin-receta-fiable-lox/\nhttp://amusecandy.com/blogs/post/73504\nhttp://www.mauiwowie.xxx/social/blogs/post/8529\nhttp://diduknow.allrh.com/blogs/post/15129\nhttp://www.ocperio.org/group3/blogs/post/27454\nhttp://lifestir.net/blogs/post/51274\nhttp://southweddingdreams.com/index.php?do=/blog/128618/farmacia-online-donde-comprar-generico-glipizide-10mg-de-calidad-chile-comp/\nhttp://amusecandy.com/blogs/post/140890\nhttp://vash-dom.net/?option=com_k2&view=itemlist&task=user&id=3290\nhttp://saknada.com.au/ssn/blogs/post/77638\nhttp://www.uncorpoperfetto.com/social/blogs/post/61155\nhttp://www.myindiagate.com/community/blogs/post/116577\nhttp://ec2-52-8-112-222.us-west-1.compute.amazonaws.com//index.php?qa=6275&qa_1=achat-vente-thyroxine-200-mg-thyroxine-naturel-en-ligne\nhttp://southweddingdreams.com/index.php?do=/blog/89520/hydroxyzine-order-cheap-where-to-order-atarax-without-rx/\n \nOqwG6b7qK5LV

Wapov29

22 Oktober 2017 - 04:57:06

https://www.olliesmusic.com/blog/10658/generique-erythromycin-achat-ilosone-prix-boite/ http://southweddingdreams.com/index.php?do=/blog/64730/order-erythromycin-online-erythromycin-order-online-australia/ http://southweddingdreams.com/index.php?do=/blog/74653/buy-erythromycin-250mg-on-sale-how-to-buy-erythromycin-quick-shipping/ http://southweddingdreams.com/index.php?do=/blog/88243/erythromycin-order/ https://www.olliesmusic.com/blog/31471/achat-express-erythromycin-acheter-erythromycin-pour-femme-en-ligne/ buy erythromycin from an online pharmacy http://southweddingdreams.com/index.php?do=/blog/92733/low-price-erythromycin-250-mg-buy-online/