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
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
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