PDA

View Full Version : Snippet: Online Members - Personal Timeout


Morgoth
12-01-2002, 08:54 AM
I have done it! Online Members that run under a personal Timeout script. Starting page is login.asp

I have 3 pages and a db:
Login/Logout page
Display page
Timeout page
The Database layout is at the bottom:


Now your timeout page will be all over your site and included in either your config file or in a new include file. It checks to see if you are logged in by cookies (you can personalize it yourself with sessions or whatever...) and updates your loggedin time. It also checks other accounts to see if their logged in times are less then or equal to 15 minutes, if not, it changes it to false.

You will understand when you read the code (remember to include this on all the pages you want to display this on, other pages doesn't matter, it only needs to be checked and updated if someone is going to see who is online.):

<%
Set oConn = Server.CreateObject("ADODB.Connection")
StrConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db.mdb") & ";"
oConn.Open StrConn

CookieID = Request.Cookies("Login")("CookieID")
Cookieacctname = Request.Cookies("Login")("Cookieacctname")
Cookieacctpassword = Request.Cookies("Login")("Cookieacctpassword")
intTimeoutTime = 15 'In whole number minutes

If CookieID <> "" AND Cookieacctname = "valid" OR Cookieacctpassword = "valid" Then
sSQL = "UPDATE tblMembers SET LoggedinDate=#" & Now() & "# WHERE (ID=" & CookieID & ")"
Set oRS = oConn.Execute(sSQL)
End If

sSQL = "SELECT * FROM tblMembers ORDER BY ID ASC"
Set oRS = oConn.Execute(sSQL)

Do Until oRS.EOF

If DateDiff("n", oRS("LoggedinDate"), Now()) <= intTimeoutTime Then
blnLoggedin = True
Else
blnLoggedin = False
End If

ssSQL = "UPDATE tblMembers SET Loggedin=" & blnLoggedin & " WHERE (ID=" & oRS("ID") & ")"
Set ooRS = oConn.Execute(ssSQL)

oRS.MoveNext
Loop
oConn.Close
%>




Display Page (This is basic, but you might need it to view the effects):

<!--#INCLUDE FILE="timeout.asp" -->
<%
Set oConn = Server.CreateObject("ADODB.Connection")
StrConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db.mdb") & ";"
oConn.Open StrConn

sSQL = "SELECT * FROM tblMembers WHERE (Loggedin=True)"
Set oRS = oConn.Execute(sSQL)

If oRS.EOF <> True Then

Do Until oRS.EOF

Response.Write oRS("username") & "<br>"

oRS.MoveNext

Loop

End If

Response.Write "<form method=""post"" action=""login.asp?login=logout"">" & "<br>" & "<input type=""submit"" value=""Logout"">" & "<br>" & "</form>"

oConn.Close
%>


The login and logout file:

<%
Set oConn = Server.CreateObject("ADODB.Connection")
StrConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db.mdb") & ";"
oConn.Open StrConn

acctname = Request.Cookies("Login")("Cookieacctname")
acctpassword = Request.Cookies("Login")("Cookieacctpassword")

If acctname <> "valid" AND acctpassword <> "valid" Then
acctname = Request.Form("acctname")
acctpassword = Request.Form("acctpassword")

sSQL = "SELECT * FROM tblMembers WHERE (acctname='" & acctname & "' AND acctpassword='" & acctpassword & "')"
Set oRS = oConn.Execute(sSQL)

If NOT oRS.EOF Then
Response.Cookies("Login")("CookieID") = oRS("ID")
Response.Cookies("Login")("Cookieacctname") = "valid"
Response.Cookies("Login")("Cookieacctpassword") = "valid"
Response.Redirect "display.asp"
Else
%>
<form method="post" action="login.asp">
<table cellpadding="0" cellspacing="5" width="185" border="0">
<tr>
<td><font size="2">Member: </font></td><td><input type="text" name="acctname"></td>
</tr>
<tr>
<td><font size="2">Password: </font></td><td><input type="password" name="acctpassword"></td>
</tr>
<tr>
<td></td><td><input type=submit value="Submit"></td>
</tr>
</table>
</form>
<%
End If

ElseIf Request.QueryString("logout") = "true" Then
sSQL = "UPDATE tblMembers SET LoggedinDate=#01/01/2000 12:00:00AM# WHERE (ID=" & ID & ")" 'This date was choosen because It's 2 years behind.
Set oRS = oConn.Execute(sSQL)

Response.Cookies("Login")("Cookieacctname") = ""
Response.Cookies("Login")("Cookieacctpassword") = ""
Response.Cookies("Login")("CookieID") = ""
Response.Redirect "login.asp"
Else
Response.Redirect "display.asp"
End If
oConn.Close
%>



If you can't see how this works, or if you have a problem installing it, reply to this post and I will help.
If I posted something up there wrong then I will fix it and notify people.
Now this is what the db should look like:
a few fields
atleast 3.
ID field (Auto Number)
Loggedin Field (Yes/No)
LoggedinDate Field (Date)

That's all you need, but you can add what you want to personalize it.

Have fun!

Morgoth
12-01-2002, 09:23 AM
NOW YOU CAN DOWNLOAD IT!

Mhtml
12-02-2002, 06:04 AM
I am getting a few errors here and there, mostly related to the database connections, I'm just about to modify a few things and rip apart the timeout.asp page to make it go smoother.

Morgoth
12-02-2002, 06:41 AM
The files I gave the code for work absolutly perfect, there is no reason why you would have any errors with them.

Unless it's cause your not using my exact files, maybe you edited them before testing them first.

The way I wrote the code was so it would run find for me.
If you make any modfications to it that work, I would like you to post them up.

They could help me.

(Some things are a little edited because it's not EXACTLY like the code I used, but it runs the exact same no matter what.)

Mhtml
12-02-2002, 07:16 AM
You are completely right, I had accidently bumped the keyboard which inserted a few characters and made some line breaks.

I'm creating my own version though, based on yours I might ad. You can see how I have used it when I upload it to the server with my mini forum demo which is almost finished.

Morgoth
12-02-2002, 06:22 PM
Sounds like a plan.

whammy
12-02-2002, 11:52 PM
Ahh, teamwork.

If I ever start my own company I might hire you two... hehe

Morgoth
12-03-2002, 12:10 AM
I may look cheap, but I am damn well expensive!

47¢ and hour! and not a quarter of a penny less!


I told you I was expensive, I hope these meet your budget expences.


*Morgoth laughs to himself knowing whammy can never afford him*

whammy
12-03-2002, 12:39 AM
Hah... if I was to start you right now, I would pay you about $35,000 a year and expect you to learn really fast, and give raises appropriate to your knowledge... ;)

Unfortunately I'm not in a position to do that. However, if you want to be hired at 47 cents an hour, I may offer you some freelance work. ;)

Morgoth
12-03-2002, 01:19 PM
Up here in Canada (besides our igloo building) Web Programming is one of the most popular fields to be getting into, or one of the least popular, I can't remember.