There are two ways to implement this function:
1. application
Use application objects: If you are building a large community, you may need to generate an appliaction for each login ID. Although the program design will be simpler, there will be too many logged-in users and it will consume server resources. This is not recommended here because the appliaction object It is easy to generate when the user logs in, but to truly release it completely when the user logs out of the system, I have not seen a better method so far~
<%
.....Get the username username.....
ifApplication(username)<>then
response.writeThe user is already logged in
response.end
endif
Application(username)=username'' stores the username of the user
%>
Add the sessiononend event to the global file, and Application(isuserlogin)=false when offline
In addition, it is necessary to detect whether the thread is hanging. There is a special method, which is a certain item in the server object.
(Reference: http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=815)
2. database+asp
It may be more complicated to do, but it is suitable for systems with a large number of logged-in users.
First create a database for the user - use access to create a new onlyTOL8.mdb
Data Table 1: users stores user registration information
The following data tables are set up: uID (automatic number) userName (character type) userPass (character type)
Data Table 2: onlyLogin stores user temporary login information
The following data tables are set up: OLname (character type) OLtime (date type) OLip (character type)
After the database is built, manually add data to the users table, add TOL8 to the userName table, and add 111 to the userPass table.
Next, create the user login interface. Copy the following code and save it into the onlyLogin.asp file.
<html>
<head>
<meta http-equiv=Content-Type content=text/html; charset=gb2312>
<title>It is prohibited to log in to the same account from different regions at the same time</title>
</head>
<body>
<form name=form1 method=post action=loginPost.asp>
Username: <input name=userName type=text id=userName size=15 maxlength=5>
Password: <input name=userPass type=password id=userPass size=15 maxlength=15>
<input type=submit name=Submit value=Login>
</form>
</body>
</html>
After completion, create a new loginCONN.asp file, copy the following code and save it to connect to the database.
<%
Dim CONN_TOL8
Dim Conn_T
Dim mmdd
mmdd=onlyTOL8.mdb
Set CONN_TOL8 = Server.CreateObject(ADODB.Connection)
Conn_T=Provider=Microsoft.Jet.OLEDB.4.0;Data Source= & Server.MapPath(&mmdd&)
on error resume next
CONN_TOL8.Open Conn_T %>
Next, create a loginPost.asp file that also exists in this directory. This is the key. Look carefully at the following code:
<!--#include file=loginCONN.asp -->
<%
''Delete users active within maxTime time, maxTime has been defined in the loginCONN.asp file
Conn_TOL8.Execute(Delete From onlyLogin where DATEDIFF(''s'',OLtime, now()) > & maxTime & )
''================================================ ================
Dim rs, ts, txt, sql, userName, userPass
if Request.Form(Submit)=Login then
userName=Request.Form(userName)'' Get form user login name
userPass=Request.Form(userPass)'' Get form user login password
''Since we are not discussing security issues here, user passwords are not encrypted.
Set rs = Server.CreateObject(ADODB.RECORDSET)
sql=SELECT * FROM users where userName = '' & userName & '' and userPass = '' & userPass & ''
rs.Open sql, CONN_TOL8,1,1
IF not rs.eof then
Call isOK(userName) '' If the username and password are correct, call this process. isOK will be customized in the following program.
else
Response.Write(<a href=javascript:history.go(-1)>Wrong username or password</a>)
Response.End()
end if
rs.Close
Set rs=Nothing
end if
Sub isOK(userName)
Dim Olip '' IP saved by the current login username in the database
Dim Oltime '' The last time the web page was refreshed when the current login user name is saved in the database is important data for calculating whether the user is online.
Dim OLip1 '' records the current user's login IP, used to distinguish whether it is the same user.
OLip1=Request.ServerVariables(REMOTE_ADDR)''Get the IP of the user who submitted login information
Set ts=Conn_TOL8.execute(Select * FROM onlyLogin WHERE OLname=''& userName & '')
if not ts.eof then '' Query the database to see if there is any login information for this user
OLtime=ts(OLtime)
OLip=ts(OLip)
if OLip1<>OLip and DateDiff(s,OLtime,now()) < maxTime then
''The previous sentence determines if the submitted login user IP is not the last recorded user IP in the database and
''If the difference between the user's last activity time and the current time does not exceed the specified number of seconds, it is confirmed that the user is currently online.
Response.Write <a href=javascript:history.go(-1)>This user is currently online. You cannot log in to this account from other places! </a>
Response.End()
else
'' Otherwise, it will be determined that the login is successful and the value will be paid to the session.
Session(lgName)=userName
Session(lgPass)=userPass
Response.Redirect loginOK.asp
Response.End
end if
else
''If the database does not have a login user record, execute the following statement
Dim ls
Set ls=Server.CreateObject(ADODB.RECORDSET)
ls.OpenSelect * From onlyLogin,CONN_TOL8,2,2
ls.ADDNEW
ls(OLname)=userName
ls(OLip)=OLip1
ls(OLtime)=NOW()
ls.UPDATE
ls.Close
Set ls=Nothing
''Determine the login is successful and pay the value to the session
Session(lgName)=userName
Session(lgPass)=userPass
Response.Redirect loginOK.asp
Response.End
end if
End Sub %>
After successful login, the page will jump to loginOK.asp
<style type=text/css>
<!--
body {background-color: #FF9900;}
-->
</style>
<% IF Session(lgName)<> then %>
You have logged in successfully! ! ! The following is an iframe that is sneaked into the web page in order to refresh the web page at the specified time and report to the server whether you are online.
In order to facilitate distinction, we use white as the background color for the frame web page.
<iframe border=0 name=new_date marginwidth=0 framespacing=0 marginheight=0 src=loginFrame.asp
frameborder=0 noResize width=100 scrolling=no height=30 vspale=0></iframe>
<% else %>
You are not logged in
<% end if %>
The next thing to do is loginFrame.asp
<!--#include file=loginCONN.ASP -->
<% CONN_TOL8.Execute(Update onlyLogin Set OLtime=''& NOW() & '' where OLname = '' & Session(lgName) & '') %>
<html><head><meta http-equiv=refresh content=<%=(maxTime-5)%>; url=></head></html>
At this point, the program is completed. The key to this program is to determine whether the user is online.