Gambar 1. Simpel Login Menggunakan Session -Login 1 -CodeIgniter 3 |
Langkah Awal 1. sesuaikan base url
caranya bisa lihat:
https://komputasistat.blogspot.co.id/2016/08/insert-database-crud-2-codeigniter-3.html
Langkah Selanjutnya
1. Status login disimpan kedalam session jika logout maka nilai didalam session akan dihapus . Penjelasan session codeigniter dapat dilihat di https://www.codeigniter.com/userguide2/libraries/sessions.htmlLogin.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'); } public function index() { $this->load->view('login_form'); } public function authentifikasi() { $password='admin1'; $username='komputasistat'; $array_items = array('username' => $username); if($this->input->post('submit')=='Login' && $this->input->post('username')==$username && $this->input->post('password')==$password){ $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"); //echo "string".$this->input->post('submit'); } public function halamanadmin(){ $this->load->view('halaman_admin'); } }
2.
login_form.php (..\application\views\login_form.php )
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?><!DOCTYPE html> <html lang="en"> <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>
3. Halaman admin hanya bisa diakses apabila berstatus login. Jika tidak berstatus login maka akan dilempar ke halaman login.
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> } header('location: '.$this->config->base_url()."index.php/login/halamanadmin"); //echo "string".$this->input->post('submit'); } public function halamanadmin(){ $this->load->view('halaman_admin'); } }
Gambar 2. halaman admin |
Trimakasih..
0 komentar:
Post a Comment