Gambar 1. Login Menggunakan Session dan MySQL Database |
Setelah sebelumnya kita telah memanfaatkan session untuk login (https://komputasistat.blogspot.co.id/2016/08/simpel-login-menggunakan-session-login.html). Pada kali ini akan menambahkan database MySQL. Database akan digunakan untuk menyimpan password dan username.
Langkah awal
Kode program ada di halaman ini merupakan hasil penambahan kode dari sebelumnya:https://komputasistat.blogspot.co.id/2016/08/simpel-login-menggunakan-session-login.html . Maka jika telalu rumit dapat mempelajari halaman sebelumnya
Kemudian
sesuaikan
1. ...\application\config\database.php
2. ...\application\config\routes.php
3. ...\application\config\autoload.php
caranya bisa dilihat pada halaman:
https://komputasistat.blogspot.co.id/2016/08/select-database-crud-1-codeigniter-3.html
4. sesuaikan juga base urlnya lihat:
https://komputasistat.blogspot.co.id/2016/08/insert-database-crud-2-codeigniter-3.html
Langkah Selanjutnya
1. SQL:
CREATE TABLE IF NOT EXISTS `user` ( `role` int(1) NOT NULL, `id_user` int(4) NOT NULL, `username` varchar(50) NOT NULL, `password` char(128) NOT NULL, `last_login` datetime NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data untuk tabel `user` -- INSERT INTO `user` (`role`, `id_user`, `username`, `password`, `last_login`) VALUES (1, 1, 'hastan', 'bbfe3eca408902d10529a5f604c4884a', '2016-08-14 00:00:00'), (2, 2, 'Oddsay', '53e62ab55d6fce87c22e763da78dab83', '2016-08-15 00:00:00'), (3, 3, 'Oddsaydev', 'c18dac620f883da1123de498c0ecdd8d', '2016-08-15 00:00:00');
* user ada 3 yaitu : 1. username: hastan
password: hastan
2. username: Oddsay
password: Oddsay
3. username: Oddsaydev
password: Oddsaydev
2.
Login.php (..\application\controllers\Login.php )
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller { function __construct(){ parent::__construct(); $this->load->helper( array('url' ,'form' )) ; $this->load->library('session'); $this->load->model('autentifikasi_models'); } public function index() { $this->load->view('login_form'); } public function authentifikasi() { if($this->input->post('submit')=='Login'&&$this->autentifikasi_models->cocokan($this->input->post('username'),$this->input->post('password'))){ $array_items = array('username' => $this->input->post('username')); $this->session->set_userdata('logged_in', $array_items ); } else if($this->input->post('submit')=='Logout') { $this->session->unset_userdata('logged_in', $array_items); } header('location: '.$this->config->base_url()."index.php/login/halamanadmin"); } public function halamanadmin(){ $this->load->view('halaman_admin'); } }
3.
autentifikasi_models.php (..\application\models\autentifikasi_models.php )
<?php class autentifikasi_models extends CI_Model{ function cocokan($username, $password){ echo"nama".$username."password".$password; $arrayName = array('username' => $username,'password' => md5($password)); $this->db->where( $arrayName); $query = $this->db->get('user'); foreach ($query->result() as $row) {return true; }}}
4.
login_form.php (..\application\views\login_form.php )
<html> <?php if (isset($this->session->userdata['logged_in']['username'])) { header('location: '.$this->config->base_url()."index.php/login/halamanadmin"); } ?> <head> </head> <body> <?php echo form_open('login/authentifikasi'); echo 'username: '.form_input('username').'<br/>'; echo 'password: '.form_password('password').'<br/>'; echo form_submit('submit', 'Login'); echo form_close(); ?> </body> </html>
5.
halaman_admin.php (..\application\views\halaman_admin.php).
<html> <?php if ($this->session->userdata['logged_in']['username']==false) { header('location: '.$this->config->base_url()); }else{ $username = $this->session->userdata['logged_in']['username']; } ?> <body> <h1>Selamat Datang, <?php echo $username ?></h1> <h3>Halaman ini hanya bisa diakses oleh admin</h3> <?php echo form_open('login/authentifikasi'); echo form_submit('submit', 'Logout'); echo form_close(); ?> </body> </html>
Sekian.. Trimakasih
0 komentar:
Post a Comment