# Redirection Error

**URL:** https://discourse.cakephp.org/t/redirection-error/9175
**Category:** Need Help
**Created:** [March 27, 2021, 11:18pm UTC](https://discourse.cakephp.org/t/redirection-error/9175 "2021-03-27T23:18:32Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)
#### Post date: [March 27, 2021, 11:18pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/1 "2021-03-27T23:18:32Z")

</div>

I am using cakephp version 4. When I try to load my project, I get the error: This page isn’t working. localhost redirected you too many times ERR\_TOO\_MANY\_REDIRECTS. So I googled the problem and found some debugging code. The site I found said to put the following in AppController:

```
public function redirect($url) { 
	debug($url); 
	debug(stackTrace()); 
	die; }

```

When I paste in the code and refresh my browser, I get the error:

Fatal error: Declaration of App\Controller\AppController::redirect($url) must be compatible with Cake\Controller\Controller::redirect($url, int $status = 302): ?Cake\Http\Response in C:\xampp\htdocs\momp\src\Controller\AppController.php on line 31

line 31 is: class AppController extends Controller

---

<div class="post-metadata">

### Author: ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)
#### Post date: [March 27, 2021, 11:35pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/2 "2021-03-27T23:35:28Z")

</div>

I changed the function parameters to `	public function redirect($url, int $status = 302)` but I got the same error.

---

<div class="post-metadata">

### Author: ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)
#### Post date: [March 28, 2021, 4:42am UTC](https://discourse.cakephp.org/t/redirection-error/9175/3 "2021-03-28T04:42:26Z")

</div>

The `: ?Cake\Http\Response` is part of the function declaration; it’s specifying that the return type of the function will be a Response object (or possibly null, indicated by the “?”).

---

<div class="post-metadata">

### Author: ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)
#### Post date: [March 28, 2021, 4:43am UTC](https://discourse.cakephp.org/t/redirection-error/9175/4 "2021-03-28T04:43:25Z")

</div>

But the “too many redirects” is usually a result of the login page not being excluded from authentication, so it redirects to the login page, which redirects to the login page, etc.

---

<div class="post-metadata">

### Author: ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)
#### Post date: [March 28, 2021, 1:20pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/5 "2021-03-28T13:20:33Z")

</div>

This is what I have in my UsersController. I baked it so it should be correct.

```
public function login()
{
    $this->request->allowMethod(['get', 'post']);
    $result = $this->Authentication->getResult();
	//var_dump($result);
    // regardless of POST or GET, redirect if user is logged in
    if ($result->isValid()) {
        // redirect to /mealplans after login success
        $redirect = $this->request->getQuery('redirect', [
            'controller' => 'Mealplans',
            'action' => 'index',
        ]);

        return $this->redirect($redirect);
    }
    // display error if user submitted and authentication failed
    if ($this->request->is('post') && !$result->isValid()) {
        $this->Flash->error(__('Invalid email or password'));
    }
}
```

---

<div class="post-metadata">

### Author: ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)
#### Post date: [March 28, 2021, 1:48pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/6 "2021-03-28T13:48:56Z")

</div>

you need to add the following your userscontroller:

> public function beforeFilter(\Cake\Event\EventInterface $event)  
> {  
> parent::beforeFilter($event);  
> // Configure the login action to not require authentication, preventing  
> // the infinite redirect loop issue  
> $this-\>Authentication-\>addUnauthenticatedActions([‘login’]);  
> }

see [CMS Tutorial - Authentication - 4.x](https://book.cakephp.org/4/en/tutorials-and-examples/cms/authentication.html#adding-login)

---

<div class="post-metadata">

### Author: ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)
#### Post date: [March 28, 2021, 1:56pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/7 "2021-03-28T13:56:29Z")

</div>

Thanks, dirk. I had that in my UsersController already. I tried baking UsersController again but it didn’t create a login or logout method. I wonder why that is.

---

<div class="post-metadata">

### Author: ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)
#### Post date: [March 28, 2021, 2:22pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/8 "2021-03-28T14:22:10Z")

</div>

I don’t have password\_confirm encrypted in my database. Could this be a problem?

---

<div class="post-metadata">

### Author: ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)
#### Post date: [March 28, 2021, 2:25pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/9 "2021-03-28T14:25:56Z")

</div>

The authentication plugin will be expecting that the password in the database is hashed (not encrypted). If it’s not, it won’t let you log in.

---

<div class="post-metadata">

### Author: ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)
#### Post date: [March 28, 2021, 2:29pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/10 "2021-03-28T14:29:19Z")

</div>

baking does not generate the login, logout or register action. You should have noticed that, when you first baked the userscontroller.

only you know which steps you followed to get the login action in your controller, so you have to check against the tutorial what is done and what is missing. [CMS Tutorial - Authentication - 4.x](https://book.cakephp.org/4/en/tutorials-and-examples/cms/authentication.html)

---

<div class="post-metadata">

### Author: ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)
#### Post date: [March 28, 2021, 2:33pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/11 "2021-03-28T14:33:24Z")

</div>

what is the function of your field passwordconfirm?

---

<div class="post-metadata">

### Author: ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)
#### Post date: [March 28, 2021, 2:46pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/12 "2021-03-28T14:46:44Z")

</div>

When the user registers for an account, he’s asked to confirm his password.

---

<div class="post-metadata">

### Author: ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)
#### Post date: [March 28, 2021, 4:22pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/13 "2021-03-28T16:22:21Z")

</div>

i think there is no need for this database field.  
here is a short description of how password hashing works in cakephp 3 and cakephp 4 and a solution to your problem.

> <https://stackoverflow.com/questions/25355687/cakephp-3-0-alpha2-how-to-compare-new-password-to-old-passwords/25356665#25356665>

---

<div class="post-metadata">

### Author: ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)
#### Post date: [March 28, 2021, 4:35pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/14 "2021-03-28T16:35:36Z")

</div>

When I comment out the lines of code in my login method in UsersController, the page loads with no problem so something is wrong with my login method. I got the code for the method from the cakephp 4 tutorial.

---

<div class="post-metadata">

### Author: ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)
#### Post date: [March 28, 2021, 4:38pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/15 "2021-03-28T16:38:57Z")

</div>

I’m stepping through the login method now and uncommenting it out line by line.

---

<div class="post-metadata">

### Author: ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)
#### Post date: [March 28, 2021, 4:46pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/16 "2021-03-28T16:46:31Z")

</div>

When I uncomment r`eturn $this->redirect($redirect);` it displays ERR\_TOO\_MANY\_REDIRECTS again so that’s the line of code that is guilty.

---

<div class="post-metadata">

### Author: ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)
#### Post date: [March 28, 2021, 4:56pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/17 "2021-03-28T16:56:22Z")

</div>

I tried removing the controller and action from the redirect but it still didn’t speed up the page load.

---

<div class="post-metadata">

### Author: ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)
#### Post date: [March 28, 2021, 5:04pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/18 "2021-03-28T17:04:56Z")

</div>

what is the output when you put these two lines before the redirect ?

debug ($this-\>request-\>getAttribute(‘authentication’));  
die();

---

<div class="post-metadata">

### Author: ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)
#### Post date: [March 28, 2021, 5:10pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/19 "2021-03-28T17:10:15Z")

</div>

object(Authentication\AuthenticationService) id:0 {protected \_authenticators =\> object(Authentication\Authenticator\AuthenticatorCollection) id:1 { }protected \_identifiers =\> object(Authentication\Identifier\IdentifierCollection) [id: 2](http://localhost/users/login?redirect=%2Fmealplans%2Findex#cake-db-object-6060b851e13869.04591855-2) {}protected \_successfulAuthenticator =\> object(Authentication\Authenticator\SessionAuthenticator) [id: 4](http://localhost/users/login?redirect=%2Fmealplans%2Findex#cake-db-object-6060b851e13869.04591855-4) {}protected \_result =\> object(Authentication\Authenticator\Result) id:6 { }protected \_defaultConfig =\> []protected \_config =\> []protected \_configInitialized =\> true}

---

<div class="post-metadata">

### Author: ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)
#### Post date: [March 28, 2021, 6:14pm UTC](https://discourse.cakephp.org/t/redirection-error/9175/20 "2021-03-28T18:14:42Z")

</div>

I tried: return $this-\>redirect($this-\>referer()); and I still got the redirection error.

[Next page](https://discourse.cakephp.org/t/redirection-error/9175.md?page=2)
