...

View Full Version : Session time out



ecnarongi
08-24-2004, 04:15 PM
I need to set a 15 minute window for someone to log into my site, and after 15 minutes they can no longer go to any other pages. Plus I need a counter to count down how much time they still have left. I am not sure if sessions will handle this, but after the 15 minutes are up from the time you enter the site I want all functions of the site to cease. Please help, all help is appreciated. Thank you.

Morgoth
08-24-2004, 08:48 PM
This can not be done with sessions because sessions are rewriting and updated when you refresh the page.

I suggest keeping track of the user by IP address in a database. This way you know who he is, when they first clicked on the link to give them 15 minutes free and how long he has left.

Code for finding out IP:


UserIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If UserIP = "" Then
UserIP = Request.ServerVariables("REMOTE_ADDR")
End If
Sometimes IPs are hidden behind proxy servers, this will help keep the IPs unique as possible.

You also store the time and date. Then you use (everytime he refreshes a page) a script which checks the date from when he first joined.


DateDiff("n", theDate, Now())
"n" = minutes
theDate = The date in the database.
Now() = The time when the script is ran.


If DateDiff("n", theDate, Now()) >= 15 Then
Response.Write "You're 15 minutes are up"
Response.End
End If

Does this help?

For more information about the DateDiff() function.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsfctdatediff.asp

ecnarongi
08-24-2004, 09:40 PM
This helps plenty, but what if I want a ticker count down how much time is left how would I do that? Please let me know what you think. Also, how do I kick him/her off my site after 15 minutes? All help is appreciated.

Morgoth
08-24-2004, 10:32 PM
The counter is easy, you use the DateDiff() function again along with it's close personal friend the DateAdd() function, this time will have count down code. You can find many different count down scripts on aspin.com or even google.com.

I changed the idea,


<%
strDoneDate = DateAdd("n", 15, theDate)

If DateDiff("n", theDate, Now()) >= 15 Then
'This is how to kick the user from your site, he wont be able to come back, unless he changes his IP.
Response.Redirect("kick.asp")
Else
StrCDDate = Now()
strMinutes = DateDiff("n", StrCDDate, strDoneDate) - 1
StrCDDate = DateAdd("n", strMinutes, StrCDDate)
strSeconds = DateDiff("s", StrCDDate, strDoneDate) - 31
Response.Write "You have <b>"
Response.Write strMinutes & " minute</b>(<b>s</b>)<b>, and "
Response.Write strSeconds & " second</b>(<b>s</b>) "
Response.Write "left until you are kicked"
End If
%>

theDate = The Date from the database (when the user joined)

Enjoy!

ecnarongi
08-26-2004, 11:50 PM
Thanks for all your help. I do have one question, if you a high starting time like 12:59:59 the function you will have a negative number in the seconds at times. How can I avoid this issue?

Morgoth
08-27-2004, 12:24 AM
When did this problem occur? Before you tested it yourself, or after you implamented into your code?
I tried to force this error myself, I couldn't get it to show up.

ecnarongi
08-27-2004, 11:54 PM
interestingly enough this error does not occur anymore, but if you refresh or go to another page (that has the same script on it) at 59 seconds there is an error (NAN instead of a number for the seconds). Plus why do you add the -1 and -31 at the end of the calculations for minutes and seconds respectfully?

Morgoth
08-28-2004, 10:21 PM
interestingly enough this error does not occur anymore, but if you refresh or go to another page (that has the same script on it) at 59 seconds there is an error (NAN instead of a number for the seconds). Plus why do you add the -1 and -31 at the end of the calculations for minutes and seconds respectfully?

I am not sure exactly, but I know if you change them you will not have the correct time. I am 100% sure that, that would be your problem, but I can't find away around using the extra numbers.

Maybe you can write an if statement to prevent the negative numbers. Like, if Seconds < 1 Then something or other.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum