Forum Moderators: open

Message Too Old, No Replies

Session Variable

How to?

         

Shuvi

6:47 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



Can someone tell me what is the best way to make a session timeout and how to have a message on the login page that say "your session has expired. Please login again."
thank you

txbakers

7:36 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When a person logs in, I create a session variable as such:

<% Session ("user") = their name %>

Then, on every page within the site, I include a simple timeout.asp which looks like this:

<% if String(Session("user")) == "undefined") {
Response.Write("<script type='text/javascript'>alert('You have timed out, please login again.');</script");
Response.Redirect("login.asp");
} else {

%>

Then, at the bottom of the page I include timeoutclose.asp which looks like this:

<% } %>

That's pretty simple.

Shuvi

7:52 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



How does the session("user") change from someone's name to "undefined"?
I know that you have to put session.timeout in your global.asa. But I don't understand what happens to the session once it times out.

Thanks for you help!

mattglet

7:58 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What txbakers is doing, is checking the status of the session variable, and converting it to a string. If the session has timed out, there will be no more value stored in the session variable, making it null. If the String() function encounters a null value, it returns "undefined".

So, if your session is empty, and the String() function tries to convert it to a string, it will return "undefined" as the value, and make the IF statement TRUE, therefore setting off the javascript alert, and redirecting to the login.asp page.

Sorry if that's still confusing.

-Matt

Shuvi

8:02 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



I'm testing it right now.
What will happen if I do the following?
if session("user") = "" then
//alert
//redirect
end if

vs.

if session("user") == "undefined" then
//alert
//redirect
end if

I think I may be making this more difficult on myself than it really is. But I really appreciate all the help!
Thank you

mattglet

8:04 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if session("user") = "" then
//alert
//redirect
end if

will work.

-Matt

Shuvi

8:20 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



Maybe this is a wrong question to be asking in this forum but I'll try anyway.

When I do the alert and then redirect, it doesn't show my alert it simple redirect. I want it to alert and redirect when the user clicks "ok".
Can I do that?

Thanks!

txbakers

8:38 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can't alert in ASP.

You can only alert in client side javascript, so you have to do what I did with Response.Write() the entire javascript routine to cause the alert.

Shuvi

8:46 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



I got it work!
Thanks for all your help everyone!