-1

I am working on a project using polymer 3 where I want to destroy/clear a cookie and log out the user from the website. Destroying or clearing cookie is not an issue but I am just not able to detect when the browser closes.

So far I have tried:

    window.onbeforeunload

from the JavaScript end and for php have tried this:

    session_set_cookie_params(0);

but none are working. Any help would be really appreciable. I can use setInterval() or setTimeout() function but it's not fulfilling my requirement.

Plz don't suggest any jQuery suggestion.

sayalok
  • 882
  • 3
  • 15
  • 30
  • 1
    Possible duplicate of [Detect browser or tab closing](https://stackoverflow.com/questions/3888902/detect-browser-or-tab-closing) – Saad Suri Nov 19 '18 at 06:59
  • please read my question first – sayalok Nov 19 '18 at 07:00
  • 7
    you don't get the information you want, because it's none of your buisness. All the browser will tell you is wether your page is closing. Wether this is because someone closed the tab, or the entire browser, is not your buisness. The same way that it is not your buisness what other tabs are open in the browser. – Thomas Nov 19 '18 at 07:15
  • *"I want to destroy/clear a cookie and log out the user from the website"* the use a session cookie, and declare a Session as terminated after let's say 30 minutes of inactivity. And if it's the kind of website where you often have long periods of inactivity by the user, then mybe you want to implement a ping every 5 minutes, just to tell the server "I'm still here" – Thomas Nov 19 '18 at 07:20
  • 1
    I've read your question that's why I flagged it as a duplicate. You want to use ajax on your browser close event which you can find in this answer https://stackoverflow.com/questions/3888902/detect-browser-or-tab-closing#answer-44057659 – Saad Suri Nov 19 '18 at 07:21
  • @SaadSuri i definitely check out those que and ans when i didnt get anything then i post a que here. i really dont wannt to disturb other with a bad que and also harm my reputation. i also mention what i tried so far – sayalok Nov 19 '18 at 08:07
  • @sayalok Try session cookies http://php.net/manual/en/function.session-set-cookie-params.php – Znaneswar Nov 19 '18 at 09:47
  • @misorude i consider it and that is why i asked for help here. – sayalok Nov 19 '18 at 10:01
  • @Znaneswar tried this one also before even posting que here – sayalok Nov 19 '18 at 10:02
  • I found this: https://stackoverflow.com/questions/3888902/detect-browser-or-tab-closing It basically says: "You can only detect when the page is unloaded, not when the window is closed." – Justin Schwimmer Nov 19 '18 at 06:57
  • dear @Justin Schwimmer, i tried all the stackoveflow suggestion for couple of hours. before answer u should really check my question properly. dont ans any que until u become certain about it. – sayalok Nov 19 '18 at 07:08
  • Sounds like you wanna detect when a browser is closed? Wait you said: "i just not able to detect browser close" - now I'm certain about it! Plus the title... – Justin Schwimmer Nov 19 '18 at 07:12
  • yes thats what i want – sayalok Nov 19 '18 at 07:13

2 Answers2

0

Use sessionStorage. It Works propably exactly as you want. Whenever user close browser, everything in sessionStorage is removed. Be aware that it works only when user completely close browser. Not only a tab.

Kuba Šimonovský
  • 2,013
  • 2
  • 17
  • 35
0

I would go the other way around. If the user arrives to the page for the first time, log out the user. You can easily do that by checking if a certain variable is set or not, and then set it whenever the user logs in.

Rickard Elimää
  • 7,107
  • 3
  • 14
  • 30