Guest User

Untitled

a guest
Feb 15th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. if (! $this->session->userdata('first_name'))
  2. {
  3. redirect('login');
  4. }
  5.  
  6. <?php
  7. class Current_User {
  8.  
  9. private static $user;
  10.  
  11. private function __construct() {}
  12.  
  13. public static function user() {
  14.  
  15. if(!isset(self::$user)) {
  16.  
  17. $CI =& get_instance();
  18. $CI->load->library('session');
  19.  
  20. if (!$user_id = $CI->session->userdata('user_id')) {
  21. return FALSE;
  22. }
  23.  
  24. if (!$u = Doctrine::getTable('User')->find($user_id)) {
  25. return FALSE;
  26. }
  27.  
  28. self::$user = $u;
  29. }
  30.  
  31. return self::$user;
  32. }
  33.  
  34.  
  35. public static function login($email, $password) {
  36.  
  37. // get User object by email
  38. if ($u = Doctrine::getTable('User')->findOneByEmail($email)) {
  39.  
  40.  
  41. // to ge the mutated version of the input password
  42. $u_input = new User();
  43. $u_input->password = $password;
  44.  
  45. // password match
  46. if ($u->password == $u_input->password) {
  47.  
  48.  
  49. $CI =& get_instance();
  50. $CI->load->library('session');
  51. $CI->session->set_userdata('user_id',$u->id);
  52. $CI->session->set_userdata('username',$u->username);
  53. $CI->session->set_userdata('first_name',$u->first_name);
  54. $CI->session->set_userdata('last_name',$u->last_name);
  55.  
  56. self::$user = $u;
  57.  
  58. return TRUE;
  59. }
  60.  
  61. unset($u_input);
  62. }
  63.  
  64. // login failed
  65. return FALSE;
  66.  
  67. }
  68.  
  69.  
  70. public function __clone() {
  71. trigger_error('No duplicates allowed.', E_USER_ERROR);
  72. }
  73.  
  74. }
  75.  
  76. $CI->session->set_userdata('logged_in', 'TRUE');
  77.  
  78. if (! $this->session->userdata('logged_in')==TRUE)
  79. {
  80. redirect('login');
  81.  
  82. <?php
  83. class MY_Controller extends Controller
  84. {
  85. function __construct()
  86. {
  87. parent::Controller();
  88. if (! $this->session->userdata('first_name'))
  89. {
  90. redirect('login'); // the user is not logged in, redirect them!
  91. }
  92. }
  93. }
  94.  
  95. class Secret_page extends MY_Controller {
  96.  
  97. // your logged in specific controller code
  98. }
  99.  
  100. /*
  101. | -------------------------------------------------------------------
  102. | Native Auto-load
  103. | -------------------------------------------------------------------
  104. |
  105. | Nothing to do with cnfig/autoload.php, this allows PHP autoload to work
  106. | for base controllers and some third-party libraries.
  107. |
  108. */
  109. function __autoload($class)
  110. {
  111. if(strpos($class, 'CI_') !== 0)
  112. {
  113. @include_once( APPPATH . 'core/'. $class . EXT );
  114. }
  115. }
  116.  
  117. Admin_Controller.php
  118. MY_Controller.php
  119. Public_Controller.php
  120.  
  121. $user_session = $this->model_user_lite->Session_Check();
  122. if ( $user_session == FALSE )
  123. {
  124. redirect('main/about');
  125. exit();
  126. }
  127. $data['auth'] = $user_session;
  128. $user_id = $this->model_user_lite->Session_UserID ();
  129.  
  130. function Session_Check ( $return_link = FALSE )
  131. {
  132.  
  133. $auth = $this->session->userdata('logged_in');
  134. if ( $auth == FALSE )
  135. {
  136. $return_value = FALSE;
  137. }
  138. else
  139. {
  140. $return_value = TRUE;
  141. }
  142.  
  143. return $return_value;
  144.  
  145. }
Add Comment
Please, Sign In to add comment