如何在没有圆形的cakephp中循环重定向的情况下重定向X码进行X码失败?
-
29-09-2019 - |
题
我正在使用核心身份验证组件。我创建了一个管理所有权限的用户登录。我实施登录监视的方式是检查 $this->Auth->user()
在app_controller中。每次app_controller循环 beforeFilter()
功能和 !$this->Auth->user()
, ,它将增加 Captcha.LoginAttempts
会话变量。什么时候 Captcha.LoginAttempts
IS> 3,我希望它重定向到验证码控制器,显示验证码屏幕,要求用户确认他们是人类。 (类似于Stackoverflow的方式)。
我遇到的问题是,如果我在页面上的蛋糕框架中使用某个元素或引用某些内容,它将触发重定向并导致无数的圆形重定向。有更好的方法实施吗?
这是我一直在弄乱的实际代码。但这基本上很烂(IMO):
// this is in the app_controller beforeFilter() method.
if($this->Auth->user()) {
$this->Session->delete('Captcha');
} else {
$this->Session->write('Captcha.LoginAttempts', $this->Session->read('Captcha.LoginAttempts') + 1);
if ($this->Session->read('Captcha.LoginAttempts') > 3) {
if (!$this->Session->read('Captcha.controller')) {
$this->Session->write('Captcha.controller', $this->params['controller']);
$this->Session->write('Captcha.action', $this->params['action']);
}
if ($this->Session->read('Captcha.fail') !== 'true') { // avoid circular reference redirects
$this->Session->write('Captcha.fail', 'true');
$this->redirect(array('controller' => 'captchas', 'action' => 'index'));
}
}
}
您可以看到我如何尝试避免循环参考。但是然后用户可以转到登录页面,因为 Captcha.fail
会话变量已经设置,它将忽略重定向。必须有一种更优雅的方式来实施此操作。任何人?
解决方案
通常,我只是尝试回答您尝试这样做的方式,但是由于您要求提供任何更好的想法,所以我要做的是在登录页面上实际上将CAPTCHA在登录页面上实际上使用,并使用authcomponents indentent in holdim in holdim and offerions和loginredirect,例如loginredirect,自动取向,并允许()。然后,只需根据验证码打开/关闭验证码即可。
对于您当前的方法,我认为您不会获得优雅的方法。但是,您也许可以更改AuthComponent的属性以获取所需的内容。您可以更改登录和登录功能,以便 /CATPCHAS /INDEX是新的登录表单,然后在成功的CATPCHA上,将登录名设置为 /用户 /登录或其他内容。这样,如果某人尝试直接击中 /用户 /登录而无需进行验证码,则AuthComponent Logic将启动并重定向到 /CAPTCHAS /INDEX。
这是一些相关的手册页:
- http://book.cakephp.org/view/392/loginredirect
- http://book.cakephp.org/view/382/allow
- http://book.cakephp.org/view/395/autoredirect
- http://book.cakephp.org/view/391/loginaction
希望这可以帮助!