Guest User

Untitled

a guest
Apr 25th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. function index() {
  2.  
  3. if ($this->session->userdata('logged_in')) {
  4. $this->load->view('main');
  5.  
  6. } else {
  7. redirect('/login');
  8. }
  9. }
  10.  
  11. function archive() {
  12.  
  13. if ($this->session->userdata('logged_in')) {
  14.  
  15. function __construct()
  16. {
  17. parent::__construct();
  18. if ( ! $this->session->userdata('logged_in'))
  19. {
  20. // Allow some methods?
  21. $allowed = array(
  22. 'some_method_in_this_controller',
  23. 'other_method_in_this_controller',
  24. );
  25. if ( ! in_array($this->router->fetch_method(), $allowed)
  26. {
  27. redirect('login');
  28. }
  29. }
  30. }
  31.  
  32. // Create file application/core/MY_Controller.php
  33. class Auth_Controller extends CI_Controller {
  34.  
  35. function __construct()
  36. {
  37. parent::__construct();
  38. if ( ! $this->session->userdata('logged_in'))
  39. {
  40. redirect('login');
  41. }
  42. }
  43. }
  44.  
  45. <?php
  46. defined('BASEPATH') OR exit('No direct script access allowed');
  47. class MY_Controller extends CI_Controller {
  48.  
  49. function __construct()
  50. {
  51. parent::__construct();
  52. $CI = & get_instance();
  53. $CI->load->library('session');
  54. $CI->load->helper('url');
  55. if ( !$this->session->userdata('logged_in'))
  56. {
  57. redirect('login');
  58. }
  59. }
  60.  
  61. function __construct() {
  62. parent::__construct();
  63. $CI = & get_instance();
  64. $CI->load->library('session');
  65. $CI->load->helper('url');
  66. // echo "<pre>";print_r($this->router);echo "</pre>";
  67.  
  68. /**
  69. * if webmaster then check admin session else check user session
  70. * But there may be some classes's method that doesn't requires login hence it is also need to check if
  71. * current request is for those methods before checking session
  72. */
  73. //to use $this->config->item('webmaster_name') this you have to define
  74. // $config['webmaster_name'] = "webmaster"; in config.php file
  75.  
  76. if ($this->router->module == $this->config->item('webmaster_name')) {
  77. if (!$this->session->userdata('admin')['id']) {
  78. redirect($this->config->item('webmaster_name').'/login');
  79. }
  80. } else {
  81. if (!$this->session->userdata('user')['id']) {
  82. redirect('login');
  83. }
  84. }
  85. }
  86.  
  87. function __construct() {
  88. parent::__construct();
  89. $CI = & get_instance();
  90. $CI->load->library('session');
  91. $CI->load->helper('url');
  92.  
  93. //echo "<pre>"; print_r($this->router);echo "</pre>"; //_pr($this->config->item('excluded_auth'));
  94. /**
  95. * if webmaster then check admin session else check user session
  96. * But there may be some classes's method that doesn't requires login hence it is also need to check if
  97. * current request is for those methods before checking session
  98. */
  99. if ($this->router->module == $this->config->item('webmaster_name')) {
  100. if (!$this->session->userdata('admin')['id']) {
  101. redirect($this->config->item('webmaster_name') . '/login');
  102. }
  103. } else {
  104. if (array_key_exists($this->router->class, $this->config->item('exclude_auth')) && in_array($this->router->method, $this->config->item('exclude_auth')[$this->router->class])) {
  105. //echo "escape this method. don not validate for a session";
  106. } else {
  107. if (!$this->session->userdata('user')['id']) {
  108. redirect('login');
  109. }
  110. }
  111. }
  112. }
  113.  
  114. //save file in application/config/without_auth_methods.php
  115.  
  116. <?php
  117. defined('BASEPATH') OR exit('No direct script access allowed');
  118. $config['exclude_auth']['news'] = array('index', 'view');
  119. $config['exclude_auth']['users'] = array('index');
  120.  
  121. protected function isAuthorized()
  122. {
  123.  
  124. switch ( strtolower( $this->router->class ) )
  125. {
  126. case 'pages':
  127. $disallowLoggedOut = array( 'dashboard' );
  128. $disallowLoggedIn = array( 'index' );
  129. break;
  130.  
  131. case 'users':
  132. $disallowLoggedOut = array( 'logout' );
  133. $disallowLoggedIn = array( 'register', 'login' );
  134. break;
  135. }
  136.  
  137. if ( $this->session->userdata( 'loggedIn' ) )
  138. {
  139. if ( in_array( $this->router->method, $disallowLoggedIn ) )
  140. {
  141. redirect( 'pages/dashboard' );
  142. }
  143. }
  144. else
  145. {
  146. if ( in_array( $this->router->method, $disallowLoggedOut ) )
  147. {
  148. redirect( 'pages/index' );
  149. }
  150. }
  151. }
  152.  
  153. <?php
  154. defined('BASEPATH') OR exit('no direct access');
  155.  
  156. function isLogin($sessionType)
  157. {
  158. if(empty($_SESSION[$sessionType]))
  159. redirect(base_url('loginURL'));
  160. }
  161.  
  162. ?>
  163.  
  164. application/controllers/Access.php
  165.  
  166. defined('BASEPATH') OR exit('access denied');
  167. class Access Extends CI_Controller
  168. {
  169. funcrion __construct()
  170. {
  171. parent::__construct();
  172. $this->load->helper('login');
  173. }
  174. function home()
  175. {
  176. isLogin();
  177. $this->load->view('home_page);
  178. }
  179. }
Add Comment
Please, Sign In to add comment