I want user to not access certain method of controller when session is not set. For this I can check session in all method and if session is set then only go to furthur else redirect to specific page. Since I have many method that I don't want user to take access if session is not set. Its bulk to go through all method and check session. Are there any shortcut way to obtain this functionality.
I tried checking session is constructor method of controller but it works for all method. But I want only specific method to block if session is not set. How to do it.
Example:
class dashboard extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('dbmodel');
$this->load->helper('url','form');
//verified user check
if($this->session->userdata("unverified") != FALSE) {
redirect("verify_user");
}
}
//verified user check
}
Above code, redirects to verify_user controller as soon as 'unverified' session is found when user go to dashboard controller. But I want to give access to some method of dashboard controller. Not all method. Where as this code redirects whenever session is found and don't give access to any method of dashboard controller.
By default Controller always calls index method. If you want a different method, then write it in the Controller's file and specify its name while calling the function. Look at the URL, there is no method name is mentioned.
The pages class is extending the CI_Controller class. This means that the new pages class can access the methods and variables defined in the CI_Controller class ( system/core/Controller. php ). The controller is what will become the center of every request to your web application.
Defining a Default Controller CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes. php file and set this variable: $route['default_controller'] = ' Blog ';
CodeIgniter permits you to do this. Simply create sub-directories under the main application/controllers/ one and place your controller classes within them. Each of your sub-directories may contain a default controller which will be called if the URL contains only the sub-directory.
Check this It might help you
class MY_controller extends CI_controller{
function __construct() {
parent::__construct();
}
function _required_user($params =array()){
$action =$this->router->fetch_method();
if(empty($params['except']))
$params['except'] =array();
if(empty($params['only']))
$params['only'] =array();
if(count($params['except']) > 0 && in_array($action,$params['except']))
return true;
if(count($params['only']) > 0 && in_array($action,$params['only']) && $this->session->userdata('is_login'))
return true;
if($this->session->userdata('is_login'))
return true;
redirect('login');
}
}
class dashboard extends MY_Controller {
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('dbmodel');
$this->load->helper('url','form');
$this->_required_user(array('except'=>array('index')))
}
function add(){
/*
Session required
*/
}
function edit(){
/*
Session required
*/
}
function index(){
/*
no session required
*/
}
}
class content extends MY_Controller{
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('dbmodel');
$this->load->helper('url','form');
$this->_required_user(array('only'=>array('index')))
}
function add(){
/*
no Session required
*/
}
function edit(){
/*
no Session required
*/
}
function index(){
/*
session required
*/
}
}
class Myaccount extends MY_Controller{
function __construct() {
parent::__construct();
/*
for all functions session required
*/
$this->_required_user()
}
function edit(){
/*
session required
*/
}
function save(){
/*
session required
*/
}
}
You can modified _reuired_user function according to your requirement
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With