Call logout function on close browser or tab

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.

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

“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.””