Redirect Loop in CakePHP
I'm working on a CakePHP application. I'm trying to redirect the user to admin URL based on his/her IP address and for that I'm using开发者_运维问答 this code in app_controller.php
if(env('REMOTE_ADDR')=='foo') {
$this->redirect(array('action'=>'index', 'admin'=>1));
echo $html->link(__('Logout', true), array('controller'=> 'users', 'action'=>'admin_index'));
}
I'm getting a redirect loop as soon the condition matches. :(
This is because your app controller fires code before anything else. The code you've written essentially translates to
- Visit URL => AppController fires, sends you to /admin/controller/index/
- In /admin/controller/index/ AppController fires again and sends you to /admin/controller/index/
- As above
Another thing, you seem to be echoing a link after you do a redirect, this doesn't serve any purpose.
What you probably want is something like this
$url = 'Wherever you are redirecting to';
if (env('REMOTE_ADDR') == 'foo' && $this->params['url']['url'] != $url) {
$this->redirect($url);
}
Also, when you do $this->redirect(array('action'=>'index', 'admin'=>1));
you're essentially redirecting to the index action of whichever URL you are in. Is this what you're trying to do? If so, you'll need to modify your check to something like
$url = 'Wherever you are redirecting to';
if (env('REMOTE_ADDR') == 'foo' && $this->params['action'] != 'index' && $this->params['admin'] != 1) {
$this->redirect($url);
}
精彩评论