How to check if the Session expires using JavaScript

前提是你 提交于 2019-12-07 15:33:24

A typical implementation of a session requires two things:

  1. A cookie in the browser
  2. A server side record of the session associated with the number stored in the cookie

These cookies are usually set up as session cookies. They expire when the browser closes. Thus you don't need to worry about testing them to achieve this.

To find out if the session is still valid on the server, you need to (periodically) make an HTTP request to the server (you can do this with Ajax) and have the server respond with information about the life of the session.

A simple solution would be to return true or false, and then redirect (by setting location.href) if it is false.

A more efficient solution would be to return the remaining time to live for the session, and use that information to determine when next to check (and redirect if it is 0). A more

It is not necessary to do a periodic check of session is expired, the best thing you can do is you can check when event is fired on the front end, Say like before submit / before click etc. Server time is a key resources we need to manage to give better app performance.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!