Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2.  
  3. class Auth {
  4.  
  5. public $CI;
  6.  
  7. public function __construct() {
  8. $this->CI = &get_instance();
  9. }
  10.  
  11. public static $table = 'users';
  12.  
  13. public function login($u_name, $u_pass)
  14. {
  15. $where = [
  16. 'u_name' => $u_name,
  17. 'u_is_active' => 'Aktif'
  18. ];
  19.  
  20. $result = $this->CI->db->where($where)->limit(1)->get(self::$table);
  21.  
  22. if ($result->num_rows() === 1) {
  23. $data = $result->row();
  24. if (password_verify($u_pass, $data->u_pass)) {
  25. $session_data = [];
  26. $session_data['u_id'] = $data->u_id;
  27. $session_data['u_name'] = $data->u_name;
  28. $session_data['u_fname'] = $data->u_fname;
  29. $session_data['u_level'] = $data->u_level;
  30. $session_data['is_logged_in'] = TRUE;
  31. $this->CI->session->set_userdata($session_data);
  32. $this->last_logged_in();
  33. return TRUE;
  34. }
  35. return FALSE;
  36. } else {
  37. return FALSE;
  38. }
  39. }
  40.  
  41. private function last_logged_in()
  42. {
  43. $data = [
  44. 'u_last_logged_in' => date('Y-m-d H:i:s'),
  45. 'u_ip_address' => $_SERVER['REMOTE_ADDR'],
  46. ];
  47. $this->CI->db
  48. ->where('u_name', $this->CI->session->userdata('u_name'))
  49. ->update(self::$table, $data);
  50. }
  51.  
  52. public function is_logged_in()
  53. {
  54. return $this->CI->session->userdata('is_logged_in');
  55. }
  56.  
  57. public function auth()
  58. {
  59. if ($this->is_logged_in() == TRUE) {
  60. redirect('dashboard');
  61. }
  62. }
  63.  
  64. public function restrict()
  65. {
  66. if (!$this->is_logged_in()) {
  67. redirect(site_url());
  68. }
  69. }
  70.  
  71. public function is_admin()
  72. {
  73. return $this->CI->session->userdata('u_level');
  74. }
  75.  
  76. public function admin()
  77. {
  78. if ($this->is_admin() != "Administrator") {
  79. redirect('dashboard');
  80. }
  81. }
  82.  
  83. public function not_admin()
  84. {
  85. if ($this->is_admin() != "Administrator") {
  86. redirect('student');
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement