ASP Classic and JavaScript check for session statu

2019-07-17 18:54发布

问题:

I am in need of some help with trying to figure out how to go about checking the users session and seeing if they are still logged in or not.

The problem being is because i have a static page that pretty much loads everything into "tabs" using jquery. So when the user navigates the page, it really never leaves that same page they started out on. Hints my problem in trying to determine if they are still logged in or not when they click on update their information on their profile or whatnot.

回答1:

Create some arbitary communication with the server when the user "navigates the page". For example when they click a "tab" despite it not needing from the server use AJAX to ping the server anyway.

Of course in reality you will want to check the session is live anyway after all the user could stare at the "Update" button from an hour before actually pressing it.

You need both else you'll annoy users who have been busily using your app then find they're somehow timed-out anyway which will be contrary to their experiences on other sites.

In ASP you would store something in the Session object to indicate that the user is logged on. Here is an example "Am I logged on" page:-

<%
Response.ContentType = "application/json"
If Session("LoggedOn") Then
   Response.Write "{loggedOn: true}"
Else
   Response.Write "{loggedOn: false}"
End If
%>

Now you can use this page as your ping target. Ping just before update. If logged off I would suggest that you post details for update to the server any where but not to action the update. Request user loggon in your usual way with a redirect to a page that will then action those details.



回答2:

I would recommend that you take a slightly different approach.

Design your server side data-handling routines to return a special status, such as HTTP status 401 if the user is not logged in anymore.

Then make your client side AJAX routines capable of handling this, i.e. by opening/showing your login screen, preferably without losing the data/context of the attempted operation.



回答3:

<TITLE>YourStaticPage.aspx</TITLE>
<%
  Session("username")="StealthRT"
  Session("email")="StealthRT@stackoverflowusers.com"
  Session("userid")=1
  Session("LastPageLoad")=System.DateTime.Now
%>

When you are handling the button_click event for your 'update profile' page you should be able to check if those variables are still stored in Session.Contents("username") or Session.Contents("LastPageLoad") etc.