|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionHere is a script that track user login/logout times on a website. It's a simple script that I have used on some of the sites I've made. Also with this script you can see how many users are online at your site.
For this you need first a site with a 'login to enter' (member based community site). You
also need a database to keep the users and the records of their login/logout times.
You also need the The DatabaseThe database for this demo is a MS Access 2000 database (.mdb file) and it contains 2 tables: In the picture below you can see the design of the
Now take a look over the design of In the picture below you can see the design of the table.
This is short description of the table fields.
How this script workslogin.aspWhen a user will login on the site, the script in With the user and password entered in the login form you will build a query to to get the user's ' Get the user name and the password from the login form UserName = Request.Form ("UserName") Password = Request.Form ("Password") ... ... set conn = Server.CreateObject ("ADODB.Connection") conn.Open Application("connString") query = "SELECT Id FROM Members WHERE UserName='" &_ UserName & "' AND Password='" & Password &_ "'" ' Get the user id from the database set rs = conn.Execute (query) ... ... ' set the user id value from the Members table in a session variable Session("member") = rs("Id") You have the user id value in the Then we will insert a new record in the ' Modify all the records from the User_LogTime corresponding to this user. query = "UPDATE User_LogTime SET offline=True WHERE offline=False AND user_id=" &_ session("member") conn.Execute (query) ' Insert a new record in the User_LogTime table with the user_id, ' SessionID and the login time. query = "INSERT INTO User_LogTime (user_id, SID, Login_Time) " query = query &_ "VALUES (" & Session("member") & "," &_ Session.SessionID & ",#" & now() & "#)" conn.Execute (query) global.asaThe code above was called when the Session began and you got the login time and wrote it in the database.
When the user hits logout then Sub Session_OnEnd set conn = Server.CreateObject ("ADODB.Connection") conn.Open Application("connString") ' Update the record when the user logout and write the logout time ' plus it sets the user as OFFLINE. query = "UPDATE User_LogTime SET Logout_Time=#" & now() & "#, offline=True " query = query & "WHERE offline=False AND SID=" & Session.SessionID &_ " AND user_id=" & Session("member") conn.Execute (query) conn.Close set conn = Nothing End Sub That's all there is for tracking login/logout times. You have these values in the Online users
Having the query = "SELECT DISTINCT user_id FROM User_LogTime WHERE offline=False" Execute that query and the number of records in the recordset will be the number of your online users. Alternatively you can call query = "SELECT COUNT(*) as NumOnline FROM User_LogTime WHERE offline=False" To return a recordset with one record and one field ("NumOnline") that has the number of online users. See the demo application for examples. Remarks
Happy Programming!!
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||