# How to redirect on same page when user login after session expire in cake php 3.6?

**URL:** <https://discourse.cakephp.org/t/how-to-redirect-on-same-page-when-user-login-after-session-expire-in-cake-php-3-6/5172>\
**Category:** Need Help\
**Created:** [October 28, 2018, 5:51am UTC](https://discourse.cakephp.org/t/how-to-redirect-on-same-page-when-user-login-after-session-expire-in-cake-php-3-6/5172 "2018-10-28T05:51:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anujg1](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@anujg1](https://discourse.cakephp.org/u/anujg1)\
**Post date:** [October 28, 2018, 5:51am UTC](https://discourse.cakephp.org/t/how-to-redirect-on-same-page-when-user-login-after-session-expire-in-cake-php-3-6/5172/1 "2018-10-28T05:51:24Z")

</div>

Hi,

How to redirect on same page when user login after session expire in cake php 3.6?

Thanks in advance!

Regards  
Anuj

---

<div class="post-metadata">

**Author:** ![bichomen](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bichomen/32/1070_2.png) [@bichomen](https://discourse.cakephp.org/u/bichomen)\
**Post date:** [July 29, 2020, 8:32am UTC](https://discourse.cakephp.org/t/how-to-redirect-on-same-page-when-user-login-after-session-expire-in-cake-php-3-6/5172/2 "2020-07-29T08:32:08Z")

</div>

I have the same problem. Could you solve it? If so, how?

---

<div class="post-metadata">

**Author:** ![bizdev](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bizdev/32/1951_2.png) [@bizdev](https://discourse.cakephp.org/u/bizdev)\
**Post date:** [July 30, 2020, 11:56am UTC](https://discourse.cakephp.org/t/how-to-redirect-on-same-page-when-user-login-after-session-expire-in-cake-php-3-6/5172/3 "2020-07-30T11:56:14Z")

</div>

Hi @anujg1,

[Authentication plugin](https://book.cakephp.org/authentication/2/en/index.html) handles this by default, you just need to set `queryParam` config as shown in the documentation of the plugin.

**src/Application.php**

```php
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
    $service = new AuthenticationService();

    $service->setConfig([
        'unauthenticatedRedirect' => '/users/login',
        'queryParam' => 'redirect',
    ]);

    // your other code goes here...

    return $service;
}

```

With [AuthComponent](https://book.cakephp.org/4/en/controllers/components/authentication.html) which probably you’re using for CakePHP 3.\*, it’s the same it appends `redirect` query string as shown the the code [here](https://github.com/cakephp/cakephp/blob/3.next/src/Controller/Component/AuthComponent.php#L49), and when you login it checks for this query params exists as show [here in the code](https://github.com/cakephp/cakephp/blob/3.next/src/Controller/Component/AuthComponent.php#L418) and redirects accordingly.

So when session expires and you reload the page it will be default append the redirect query params.
