〓Introduction〓
Anyone with some common sense knows that it is obviously far-fetched to use ASP to do this function, because it is not instant messaging like QQ, MSN or IRC chat rooms. They all rely on refreshing the web page to change the last activity time to get whether the user is online. So don't expect the following tutorial to be perfect. I just recorded my thinking mode. If there is anything wrong, please ask the experts for advice!
-------------------------------------------------- ----------------------------------
〓Text〓
First of all, let me introduce to you my idea of achieving this effect. If you can understand after reading my idea, there is no need to read this article...
I can only think of two ways to implement this function:
1. database+asp
It may be more complicated to do, but it is suitable for systems with a large number of logged-in users.
2. application
Use the application object: If you are building a large community, you may have to generate an appliaction for each login ID. Although the program design will be simpler, there are too many logged-in users and it consumes server resources. I will never advocate it here. Because it is easy to generate the appliaction object when the user logs in, but it must be completely released when the user exits the system. So far, I have not seen a better method~
So let us take a look at how to use database + asp to solve the problem that the same account cannot log in at the same time!
First ask the user to create a database. Here we use access to create a new onlyNet118.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, we directly add data to the users table manually, add Net118 to the userName table, and add 111 to the userPass table. In order to highlight the focus of our discussion in this article and remove unnecessary nonsense, the password should not be encrypted, and the user name should be its own. Add~haha~
Okay, now there is a user database in the database. Let's make 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>Net118.COM prohibits simultaneous login of the same account from different regions</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 code below and save it! It’s connected to the database. I won’t explain this much...
<%
Dim CONN_Net118
Dim Conn_T
Dim mmdd
mmdd=onlyNet118.mdb
Set CONN_Net118 = Server.CreateObject(ADODB.Connection)
Conn_T=Provider=Microsoft.Jet.OLEDB.4.0;Data Source= & Server.MapPath(&mmdd&)
on error resume next
CONN_Net118.Open Conn_T
%>
Next we make a loginPost.asp file that also exists in this directory. This is more critical. 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_Net118.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_Net118,1,1
IF not rs.eof then
Call isOK(userName) ' Call this process if the username and password are correct, 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 'The 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 'Record 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_Net118.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_Net118,2,2
ls.ADDNEW
ls(OLname)=userName
ls(OLip)=OLip1
ls(OLtime)=NOW()
ls.UPDATE
ls.Close
Set ls=Nothing
'Determine successful login and pay value to session
Session(lgName)=userName
Session(lgPass)=userPass
Response.Redirect loginOK.asp
Response.End
end if
End Sub %>
If you can understand asp files, you will know at a glance that after successful login, the leaf will jump to loginOK.asp. Let’s take a look at the code of this leaf immediately.
<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. Welcome to the webmaster information network: http://www.Net118.com
<% end if %>
If you are a careful person, you will immediately know that what we need to do next is loginFrame.asp
<!--#include file=loginCONN.ASP -->
<% CONN_Net118.Execute(Update onlyLogin Set OLtime='& NOW() & ' where OLname = ' & Session(lgName) & ') %>
<html><head><meta http-equiv=refresh content=<%=(maxTime-5)%>; url=></head></html>
Okay, so far our program is complete. The key to this program is to determine whether the user is online, and I have no choice but to use FRAME to nest the key leaf that regularly refreshes to determine whether the user is online in the leaf of the main program. Here, in actual operation, you can change the width and height of the iframe to 0 so that ordinary users cannot see it, or make the background color of the web page of the main program the same as that of the nested regularly refreshed web page, and it will be ok.
Not long ago, I saw someone in the discussion forum saying that it can be determined using things like sessions and cookies. This is obviously impossible, because the objects they generate only work on themselves, and their data content cannot be shared with other users at all. shared. Appliaction should be another way to achieve this purpose, but I gave up this idea when I thought that if there are many users logging in at the same time, at least one to three appliaction objects will be generated for each user, because then our original poor server Will definitely be dragged down~