views:

85

answers:

2

Hello,

I got problem. I'm builing an app with KohanaPHP framework and I got rediretion endless loop.

Here's the code I put in constructor oh my main controller:

   if(empty($this->user->real_name)) {
    url::redirect('/');
   }

Any ideas how to solve that issue?

A: 

One way to solve this - if you are checking for login - is to create a custom base class that all controllers will use which require an authenticated user. This base class will redirect the user to an authentication controller if they are not authenticated. The authentication controller will not inherit from the base class, which prevents an endless redirection loop.

I have used this scheme to implement authentication in CodeIgniter, and while CI is different than Kohana, they are close enough that it should work for you as well.

Justin Ethier
Thanks for reply. I got main controller that all controllers extend. The main problem is...that I cannot provide local URL for redirection in main controller's constructor. Weird. Am I doing something wrong?
sorrko
A: 

you probably want to do something like this

$current_url = $_SERVER['REQUEST_URI'];

if ($current_url != '' || $current_url != '/') {
 if(empty($this->user->real_name)) {
    url::redirect('/');
   }
}

so that it only redirects to the home page if you are not already at the home page (note code is not tested)

bumperbox