# Call logout function on close browser or tab

**URL:** <https://discourse.cakephp.org/t/call-logout-function-on-close-browser-or-tab/3874>\
**Category:** Need Help\
**Created:** [February 5, 2018, 1:33pm UTC](https://discourse.cakephp.org/t/call-logout-function-on-close-browser-or-tab/3874 "2018-02-05T13:33:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pritesh.b](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@pritesh.b](https://discourse.cakephp.org/u/pritesh.b)\
**Post date:** [February 5, 2018, 1:33pm UTC](https://discourse.cakephp.org/t/call-logout-function-on-close-browser-or-tab/3874/1 "2018-02-05T13:33:32Z")

</div>

I want to call my users controller logout function on close browser or close that tab.  
I have used onbeforeunload() but sometimes it does not work properly. If any other solution in cakephp 3.5.

---

<div class="post-metadata">

**Author:** ![dmuenstermann](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dmuenstermann](https://discourse.cakephp.org/u/dmuenstermann)\
**Post date:** [February 6, 2018, 8:39am UTC](https://discourse.cakephp.org/t/call-logout-function-on-close-browser-or-tab/3874/2 "2018-02-06T08:39:44Z")

</div>

Hello pritesh.b,

imho the easiest and most reliable way to achieve this would be to set a very limited (e.g. 5 min) session life time and then implement a piece of js (e.g. in the default-template) which regularly calls a controller function in order to extend the session’s life time.

This way the user is logged out a few minutes after closing the tab/browser (worst case).

Best regards

---

<div class="post-metadata">

**Author:** ![Marcel](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/marcel/32/329_2.png) [@Marcel](https://discourse.cakephp.org/u/Marcel)\
**Post date:** [February 6, 2018, 3:22pm UTC](https://discourse.cakephp.org/t/call-logout-function-on-close-browser-or-tab/3874/3 "2018-02-06T15:22:12Z")

</div>

[http://php.net/manual/en/function.session-set-cookie-params.php](http://php.net/manual/en/function.session-set-cookie-params.php)

“session.cookie\_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means “until the browser is closed.””
