2011-06-22 36 views
1

我正在研究CakePHP应用程序。我想根据他/她的IP地址的用户重定向到管理URL和我使用这个代码在app_controller.php在CakePHP中重定向循环

 if(env('REMOTE_ADDR')=='foo') { 

     $this->redirect(array('action'=>'index', 'admin'=>1)); 
     echo $html->link(__('Logout', true), array('controller'=> 'users', 'action'=>'admin_index')); 
     } 

我尽快得到一个重定向循环的条件匹配。 :(

回答

5

这是因为你的应用程序控制器火灾码任何事情之前,你已经基本上编写的代码转换为

  1. 访问URL => AppController的火灾,向您发送/管理/控制/索引/
  2. 再次/管理/控制/索引/ AppController的火灾,并发送你/管理/控制/索引/
  3. 如上

另一件事,你似乎是呼应之后你一个链接做一个重定向,这不起任何作用。

你可能需要的是这样的事情

$url = 'Wherever you are redirecting to'; 
if (env('REMOTE_ADDR') == 'foo' && $this->params['url']['url'] != $url) { 
    $this->redirect($url); 
} 

而且,当你做$this->redirect(array('action'=>'index', 'admin'=>1));你基本上重定向到你是在哪个网址索引操作。这是你想要做什么?如果是这样,你需要修改您的支票类似

$url = 'Wherever you are redirecting to'; 
if (env('REMOTE_ADDR') == 'foo' && $this->params['action'] != 'index' && $this->params['admin'] != 1) { 
    $this->redirect($url); 
} 
$url = 'Wherever you are redirecting to'; 
if (env('REMOTE_ADDR') == 'foo' && $this->params['action'] != 'index' && $this->params['admin'] != 1) { 
    $this->redirect($url); 
} 
+0

您好John,感谢您的答复,我使用了上面的代码,但我仍然收到重定向循环错误。 :( –

+0

噢,对不起,它工作正常,FTP服务器没有上传文件。:P –