Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function index() {
- if ($this->session->userdata('logged_in')) {
- $this->load->view('main');
- } else {
- redirect('/login');
- }
- }
- function archive() {
- if ($this->session->userdata('logged_in')) {
- function __construct()
- {
- parent::__construct();
- if ( ! $this->session->userdata('logged_in'))
- {
- // Allow some methods?
- $allowed = array(
- 'some_method_in_this_controller',
- 'other_method_in_this_controller',
- );
- if ( ! in_array($this->router->fetch_method(), $allowed)
- {
- redirect('login');
- }
- }
- }
- // Create file application/core/MY_Controller.php
- class Auth_Controller extends CI_Controller {
- function __construct()
- {
- parent::__construct();
- if ( ! $this->session->userdata('logged_in'))
- {
- redirect('login');
- }
- }
- }
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- class MY_Controller extends CI_Controller {
- function __construct()
- {
- parent::__construct();
- $CI = & get_instance();
- $CI->load->library('session');
- $CI->load->helper('url');
- if ( !$this->session->userdata('logged_in'))
- {
- redirect('login');
- }
- }
- function __construct() {
- parent::__construct();
- $CI = & get_instance();
- $CI->load->library('session');
- $CI->load->helper('url');
- // echo "<pre>";print_r($this->router);echo "</pre>";
- /**
- * if webmaster then check admin session else check user session
- * But there may be some classes's method that doesn't requires login hence it is also need to check if
- * current request is for those methods before checking session
- */
- //to use $this->config->item('webmaster_name') this you have to define
- // $config['webmaster_name'] = "webmaster"; in config.php file
- if ($this->router->module == $this->config->item('webmaster_name')) {
- if (!$this->session->userdata('admin')['id']) {
- redirect($this->config->item('webmaster_name').'/login');
- }
- } else {
- if (!$this->session->userdata('user')['id']) {
- redirect('login');
- }
- }
- }
- function __construct() {
- parent::__construct();
- $CI = & get_instance();
- $CI->load->library('session');
- $CI->load->helper('url');
- //echo "<pre>"; print_r($this->router);echo "</pre>"; //_pr($this->config->item('excluded_auth'));
- /**
- * if webmaster then check admin session else check user session
- * But there may be some classes's method that doesn't requires login hence it is also need to check if
- * current request is for those methods before checking session
- */
- if ($this->router->module == $this->config->item('webmaster_name')) {
- if (!$this->session->userdata('admin')['id']) {
- redirect($this->config->item('webmaster_name') . '/login');
- }
- } else {
- 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])) {
- //echo "escape this method. don not validate for a session";
- } else {
- if (!$this->session->userdata('user')['id']) {
- redirect('login');
- }
- }
- }
- }
- //save file in application/config/without_auth_methods.php
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- $config['exclude_auth']['news'] = array('index', 'view');
- $config['exclude_auth']['users'] = array('index');
- protected function isAuthorized()
- {
- switch ( strtolower( $this->router->class ) )
- {
- case 'pages':
- $disallowLoggedOut = array( 'dashboard' );
- $disallowLoggedIn = array( 'index' );
- break;
- case 'users':
- $disallowLoggedOut = array( 'logout' );
- $disallowLoggedIn = array( 'register', 'login' );
- break;
- }
- if ( $this->session->userdata( 'loggedIn' ) )
- {
- if ( in_array( $this->router->method, $disallowLoggedIn ) )
- {
- redirect( 'pages/dashboard' );
- }
- }
- else
- {
- if ( in_array( $this->router->method, $disallowLoggedOut ) )
- {
- redirect( 'pages/index' );
- }
- }
- }
- <?php
- defined('BASEPATH') OR exit('no direct access');
- function isLogin($sessionType)
- {
- if(empty($_SESSION[$sessionType]))
- redirect(base_url('loginURL'));
- }
- ?>
- application/controllers/Access.php
- defined('BASEPATH') OR exit('access denied');
- class Access Extends CI_Controller
- {
- funcrion __construct()
- {
- parent::__construct();
- $this->load->helper('login');
- }
- function home()
- {
- isLogin();
- $this->load->view('home_page);
- }
- }
Add Comment
Please, Sign In to add comment