Destroy PHP session on page leaving

前端 未结 7 1211
轻奢々
轻奢々 2020-12-06 02:52

I need to destroy a session when user leave from a particular page. I use session_destroy() on the end of the page but its not feasible for me because my page h

7条回答
  •  天涯浪人
    2020-12-06 03:11

    You can't tell when a user navigates away from the page, it's simply not possible in any reliable manner.

    The best you can do is exploit how cookies work. When starting a session, you're sending a cookie to the client which identifies the client on each subsequent visit, and hence activates the associated session. It is up to the client to send this identification on subsequent visits, and it's up to the client to "forget" his identification.

    You can instruct the client to only send the cookie for certain pages, and you can instruct him to forget the cookie when closing the browser (with a lifetime of 0). This can be set using session_set_cookie_params.

    Other than that, you can simply ignore the session parameters on pages where they don't matter. You can delete the session (or certain values of it) after some time of inactivity when you assume the client has left.

提交回复
热议问题