Skip to content

Instantly share code, notes, and snippets.

@cikal
Created May 11, 2017 04:22
Show Gist options
  • Save cikal/e52f207317a3bcf5596969c628cc85d9 to your computer and use it in GitHub Desktop.
Save cikal/e52f207317a3bcf5596969c628cc85d9 to your computer and use it in GitHub Desktop.
Login session sederhana CodeIgniter3
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Auth extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('model_login');
}
public function login()
{
if (isset($_POST['submit'])) {
# proses login disini
# ambil data inputan (user & pass) dari form
$username = $this->input->post('username');
$password = $this->input->post('password');
# cek data input ada/tidak pada DB
$tes = $this->model_login->login($username,$password);
if ($tes==1) {
# jika data ditemukan set session & redirect ke home
$data = array(
'status_login' => 'oke',
'username' => $username
);
$this->session->set_userdata($data);
redirect('home','refresh');
} else {
# jika data tidak ditemukan kembali ke form login
redirect('auth/login','refresh');
}
} else {
# cek session login, jika session sebelumnya masih ada/tidak
cek_session_login();
$this->load->view('form_login');
}
}
public function logout()
{
# mengakhiri session, dan redirect ke form_login
$this->session->sess_destroy();
redirect('auth/login','refresh');
}
}
/* End of file auth.php */
/* Location: ./application/controllers/auth.php */
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------
| AUTO-LOADER
| -------------------------------------------------------------------
| This file specifies which systems should be loaded by default.
|
| In order to keep the framework as light-weight as possible only the
| absolute minimal resources are loaded by default. For example,
| the database is not connected to automatically since no assumption
| is made regarding whether you intend to use it. This file lets
| you globally define which systems you would like loaded with every
| request.
|
| -------------------------------------------------------------------
| Instructions
| -------------------------------------------------------------------
|
| These are the things you can load automatically:
|
| 1. Packages
| 2. Libraries
| 3. Helper files
| 4. Custom config files
| 5. Language files
| 6. Models
|
*/
/*
| -------------------------------------------------------------------
| Auto-load Packges
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['packages'] = array(APPPATH.'third_party', '/usr/local/shared');
|
*/
$autoload['packages'] = array();
/*
| -------------------------------------------------------------------
| Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in the system/libraries folder
| or in your application/libraries folder.
|
| Prototype:
|
| $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/
$autoload['libraries'] = array('session','database');
/*
| -------------------------------------------------------------------
| Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('url','form','login');
/*
| -------------------------------------------------------------------
| Auto-load Config files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['config'] = array('config1', 'config2');
|
| NOTE: This item is intended for use ONLY if you have created custom
| config files. Otherwise, leave it blank.
|
*/
$autoload['config'] = array();
/*
| -------------------------------------------------------------------
| Auto-load Language files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['language'] = array('lang1', 'lang2');
|
| NOTE: Do not include the "_lang" part of your file. For example
| "codeigniter_lang.php" would be referenced as array('codeigniter');
|
*/
$autoload['language'] = array();
/*
| -------------------------------------------------------------------
| Auto-load Models
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['model'] = array('model1', 'model2');
|
*/
$autoload['model'] = array();
/* End of file autoload.php */
/* Location: ./application/config/autoload.php */
<?=form_open('auth/login');?>
<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 colspan="2" align="center"><button type="submit" name="submit">LOGIN</button></td></tr>
</table>
<?=form_close();?>
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
# cek kembali session, jika blm login di redirect lagi ke form login
cek_session();
$this->load->view('v_home');
}
}
/* End of file home.php */
/* Location: ./application/controllers/home.php */
<?php
function cek_session()
{
$CI =& get_instance();
$session = $CI->session->userdata('status_login');
if ($session!='oke') {
# jika status_login tidak sesuai kriteria
redirect('auth/login','refresh');
}
}
function cek_session_login()
{
$CI =& get_instance();
$session = $CI->session->userdata('status_login');
if ($session=='oke') {
# jika status_login sesuai kriteria
redirect('home','refresh');
}
}
/* End of file login_helper.php */
/* Location: ./application/helpers/login_helper.php */
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Model_login extends CI_Model {
function login($username,$password)
{
$val = array(
'username' => $username,
'password' => md5($password)
);
# cek apakah data dari form ada/tidak di DB
$cek = $this->db->get_where('t_user', $val);
if ($cek->num_rows()>0) {
# jika data ada
return 1;
} else {
# jika data tidak ada
return 0;
}
}
}
/* End of file model_login.php */
/* Location: ./application/models/model_login.php */
CREATE TABLE IF NOT EXISTS `t_user` (
`user_id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`nama_lengkap` varchar(40) NOT NULL,
`username` varchar(20) NOT NULL,
`password` varchar(32) NOT NULL,
);
<h1>Halaman Home</h1>
<p>Link : <?=anchor('auth/logout', 'Logout');?></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment