Advertisement
yhoezt_27

Untitled

Jan 29th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. CONTOH LOGIN USER
  2.  
  3. (A) Controller
  4. function submit_user(){
  5. $username = db_clean($this->input->post('name'));
  6. $success = $this->auth->do_login($username);
  7. if ($success)
  8. {
  9. $referrer_url = $this->session->userdata('referrer_url');
  10. if ($referrer_url == '')
  11. $redirect_back = site_url('dashboard/index');
  12. else
  13. {
  14. $redirect_back = site_url($referrer_url);
  15. $this->session->unset_userdata('referrer_url');
  16. }
  17. $data = array('status' => 'sukses', 'redirect_back' => $redirect_back);
  18. }
  19. else
  20. {
  21. $data = array('status' => 'gagal');
  22. }
  23.  
  24.  
  25. echo json_encode($data);
  26. }
  27.  
  28. (B) Library/Model
  29. function do_login($username)
  30. {
  31. // cek di database, ada ga?
  32. $CI =& get_instance();
  33.  
  34. $CI->db->select('tb_user.*, tb_level.*');
  35. $CI->db->from('tb_user');
  36. $CI->db->join('tb_level','tb_user.user_level = tb_level.id_level','left');
  37. $CI->db->where('tb_user.user_username',$username);
  38.  
  39. $result = $CI->db->get();
  40. if($result->num_rows() == 0)
  41. {
  42. // username dan password tsb tidak ada
  43. return false;
  44. }
  45. else
  46. {
  47. // ada, maka ambil informasi dari database
  48. $userdata = $result->row();
  49. $session_data = array(
  50. 'level' => $userdata->user_level,
  51. 'username' => $userdata->user_username,
  52. 'namauser' => $userdata->user_nama,
  53.  
  54. );
  55. // buat session
  56. $CI->session->set_userdata($session_data);
  57.  
  58. return true;
  59. }
  60. }
  61.  
  62. (C) View
  63. <?php
  64. //header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
  65. //header("Cache-Control: post-check=0, pre-check=0", false);
  66. //header("Pragma: no-cache");
  67.  
  68. if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
  69. <div id="view">
  70. <form id="demo-form">
  71. <h1>Login Form</h1>
  72. <div>
  73. <input type="text" text-align="center" class="form-control" placeholder="No Mobile - ENTER" required id="username" />
  74. <div id="pesan-error" class="alert alert-danger"></div>
  75. </div>
  76. </form>
  77. </div>
  78.  
  79. <script>
  80. $(document).ready(function() {
  81. $("#pesan-error").hide();
  82.  
  83. $('#username').on('keydown', function(event) {
  84. if(event.which == 13) {
  85.  
  86. var user_name = $("#username").val();
  87. if ($.trim(user_name).length < 1) {
  88. $('#pesan-error').show();
  89. $('#pesan-error').html('Harap input No Mobile');
  90. }
  91. else
  92. {
  93. $.ajax({
  94. type: "POST",
  95. url: "<?php echo site_url('web/submit_user'); ?>",
  96. dataType: 'json',
  97. data: {name: user_name},
  98. beforeSend: function(e) {
  99. if(e && e.overrideMimeType) {
  100. e.overrideMimeType("application/json;charset=UTF-8");
  101. }
  102. },
  103. success: function(data){
  104. if (data.status == 'sukses')
  105. {
  106. alert('welcome to app_stock');
  107. window.location.href = data.redirect_back;
  108. }
  109. else
  110. {
  111. alert('login gagal');
  112. location.href = location.pathname;
  113. }
  114. },
  115. error: function (xhr, ajaxOptions, thrownError)
  116. { // Ketika terjadi error
  117. alert(xhr.responseText); // munculkan alert
  118. }
  119. });
  120. }
  121.  
  122. return false;
  123. }
  124. });
  125.  
  126. });
  127. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement