2

I am trying to set-up a 'post_controller_constructor' hook in CodeIgniter. The purpose is to check if a user is not logged in then redirect him back to login page.

Here is my hook configuration array :

$hook['post_controller_constructor']  = array(
   'class'     => 'Checkpoint',
   'function'  => 'check_status',
   'filename'  => 'checkpoint.php',
   'filepath'  => 'controllers'
);

And this is the class for Hook event

class Checkpoint {
var $CI;
function __construct(){
    $this->CI =& get_instance();
}
function check_status() {
    if($this->CI->router->class == 'Login'){
        return;
    } 
    if (!isset($this->CI->session)){
        $this->CI->load->library('session');
    } 
    if(!$this->CI->session->userdata('log_status')){
        //redirect(site_url('login'));
        echo "not logged in";
    }
    else{
        echo "logged in";
    }
}}

Now here are the issues :

  1. When it receives a request from the 'Login' controller it does not return from the first if statement inside check_status() function, and prints 'not logged in' before loading the view.

  2. When I try to redirect if the session userdata is not set it shows an error 'This webpage has a redirect loop' in my browser. For this reason I have commented out the redirection statement

What can I do to solve these issues?

Rivnat
  • 1,497
  • 1
  • 20
  • 34
  • I think it's just a matter of when you should call your code. and it depends on what code comes before the calling. – mamdouh alramadan Sep 21 '13 at 07:12
  • I am calling this code just after any controller is being constructed. My purpose is to ensure that a visitor can not hit any url without signing in. – Rivnat Sep 21 '13 at 07:52
  • then that will not make sense. because once the user entered your site, he will have no session then he will be redirected to the login page and your hook will check it again to see he still has no session and another redirect will happen for infinite times. – mamdouh alramadan Sep 21 '13 at 07:56
  • Exactly, There goes my first issue, why it is not returning from the 'check_status()' function when the request comes from the 'Login' controller as I checked in the first if statement? – Rivnat Sep 21 '13 at 08:05

1 Answers1

3

As I described in my comment that your problem is when you enter your site the hook checks whether you are logged in or not, so if you are not then it will redirect you to your login page which will also trigger the hook once again which will cause an infinite redirecting process. Here's a possible solution: First, assign a user_data (once the user enters your site) in your session that represents that the user is already in your site like:

$this->session->set_userdata('is_in_login_page', TRUE);

then in your hook you can check it like:

if(!$this->CI->session->userdata('log_status') &&
 !$this->CI->session->userdata('is_in_login_page') ){
    redirect(site_url('login'));
    echo "not logged in";
}

so once the user is redirected, the 'is_in_login_page' value will be set to TRUE and the server will not redirect him anymore.

it might sound silly but I think it's a good solution in your case.

NOTE: the is_in_login_page should be set to FALSE when the user is not logged in and he is not on the login page so the server will redirect him to it.

mamdouh alramadan
  • 8,349
  • 6
  • 36
  • 53
  • It works initially, for the first time. When I hit "base_url/welcome" manually for the first time it redirects to the login page. After that, when I try to hit the same url again the welcome page comes up. However I have solved this issue by following your instructions. – Rivnat Sep 21 '13 at 09:16