Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CONTOH LOGIN USER
- (A) Controller
- function submit_user(){
- $username = db_clean($this->input->post('name'));
- $success = $this->auth->do_login($username);
- if ($success)
- {
- $referrer_url = $this->session->userdata('referrer_url');
- if ($referrer_url == '')
- $redirect_back = site_url('dashboard/index');
- else
- {
- $redirect_back = site_url($referrer_url);
- $this->session->unset_userdata('referrer_url');
- }
- $data = array('status' => 'sukses', 'redirect_back' => $redirect_back);
- }
- else
- {
- $data = array('status' => 'gagal');
- }
- echo json_encode($data);
- }
- (B) Library/Model
- function do_login($username)
- {
- // cek di database, ada ga?
- $CI =& get_instance();
- $CI->db->select('tb_user.*, tb_level.*');
- $CI->db->from('tb_user');
- $CI->db->join('tb_level','tb_user.user_level = tb_level.id_level','left');
- $CI->db->where('tb_user.user_username',$username);
- $result = $CI->db->get();
- if($result->num_rows() == 0)
- {
- // username dan password tsb tidak ada
- return false;
- }
- else
- {
- // ada, maka ambil informasi dari database
- $userdata = $result->row();
- $session_data = array(
- 'level' => $userdata->user_level,
- 'username' => $userdata->user_username,
- 'namauser' => $userdata->user_nama,
- );
- // buat session
- $CI->session->set_userdata($session_data);
- return true;
- }
- }
- (C) View
- <?php
- //header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
- //header("Cache-Control: post-check=0, pre-check=0", false);
- //header("Pragma: no-cache");
- if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
- <div id="view">
- <form id="demo-form">
- <h1>Login Form</h1>
- <div>
- <input type="text" text-align="center" class="form-control" placeholder="No Mobile - ENTER" required id="username" />
- <div id="pesan-error" class="alert alert-danger"></div>
- </div>
- </form>
- </div>
- <script>
- $(document).ready(function() {
- $("#pesan-error").hide();
- $('#username').on('keydown', function(event) {
- if(event.which == 13) {
- var user_name = $("#username").val();
- if ($.trim(user_name).length < 1) {
- $('#pesan-error').show();
- $('#pesan-error').html('Harap input No Mobile');
- }
- else
- {
- $.ajax({
- type: "POST",
- url: "<?php echo site_url('web/submit_user'); ?>",
- dataType: 'json',
- data: {name: user_name},
- beforeSend: function(e) {
- if(e && e.overrideMimeType) {
- e.overrideMimeType("application/json;charset=UTF-8");
- }
- },
- success: function(data){
- if (data.status == 'sukses')
- {
- alert('welcome to app_stock');
- window.location.href = data.redirect_back;
- }
- else
- {
- alert('login gagal');
- location.href = location.pathname;
- }
- },
- error: function (xhr, ajaxOptions, thrownError)
- { // Ketika terjadi error
- alert(xhr.responseText); // munculkan alert
- }
- });
- }
- return false;
- }
- });
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement