The same account can not be logged at the same time the problem _asp Foundation

Source: Internet
Author: User
Tags current time

About Us
A bit of common sense people know that the use of ASP to do this function is obviously a bit far-fetched, because he is not like qq,msn or IRC chat room is instant messaging, he is by refreshing the page to change the last activity time to get users online, so you do not expect the following tutorial how perfect, I also just put my thinking mode record down, what wrong place also invites the Master to advise!
--------------------------------------------------------------------------------
The text of the body
First of all, I would like to introduce you to the idea of achieving this effect, if you have read my thinking after you can understand that there is absolutely 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 landing users.

2. Application
With the Application object: If you're doing a large community, you might want to generate a appliaction for each login ID, which I would never advocate if the program is designed to be simpler but has too many landing users and consumes server resources. Because the Appliaction object is easy to generate when the user logs on, but to be truly released as the user exits the system, I haven't seen a better way to date.

So let's take a look at how to use the database +asp is how to achieve the same account can not log in at the same time the problem!

First ask the user to set up a database here we use Access to create a new Onlynet118.mdb

Data table 1:users Store user registration data
Under Data table: UID (AutoNumber) userName (character type) Userpass (character type)

Data Table 2:onlylogin Store user temporary login information
Under Data table: Olname (character) oltime (date type) olip (character type)

After the database has been built, we directly add the data to the Users table add the Net118,userpass table Add 111, in order to be able to highlight the focus of our discussion in this article, remove unnecessary nonsense, so the password is not encrypted, the user name also add ~ haha ~ username

OK, now the database has a user database, the following we do user login interface, copy the following code to save into onlylogin.asp files.
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>net118.com prohibit the same account in different areas at the same time landing </title>
<body>
<form name= "Form1" method= "Post" action= "loginpost.asp" >
User name: <input name= "UserName" type= "text" id= "UserName" size= "maxlength=" "5" >
Password: <input name= "userpass" type= "password" id= "Userpass" size= "" "Maxlength=" >
<input type= "Submit" name= "Submit" value= "Login" >
</form>
</body>


When finished, copy the following code in a new loginconn.asp file to save! is connected to the database, this I do not explain more ...
<%

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

%>

Below we do a loginpost.asp file also exists in this directory, this is more critical, carefully look at the following code:
<!--#include file= "loginconn.asp"-->
<%
' Delete maxtime time internal activity user, MaxTime in loginconn.asp file already defined
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
' Because we're not talking about security here, the user's password is 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) ' username password correctly invokes the secondary procedure, IsOK will be customized in the following program.
Else
Response.Write ("<a href=javascript:history.go ( -1) > Username or password error </a>")
Response.End ()
End If
Rs. Close
Set rs=nothing
End If
Sub IsOK (UserName)
Dim olip ' Database current login username saved IP
Dim oltime ' Database The current login user name saved last refresh page time, is to calculate whether the user online important data.
Dim OLip1 ' Logs current user login IP, used to differentiate between the identity of the same user
Olip1=request.servervariables ("remote_addr") ' obtains the IP of the user submitting the login information
Set Ts=conn_net118.execute ("Select * from Onlylogin WHERE olname= '" & UserName & "")
If not ts.eof then ' query whether the database has logged information for this user
Oltime=ts ("Oltime")
Olip=ts ("Olip")
If Olip1<>olip and DateDiff ("s", Oltime,now ()) < MaxTime Then
' The last sentence determines if a login user IP is not the final record of the user IP in the database and
' The user's last active time and current time are not more than the specified number of seconds to confirm that this user is currently online
Response.Write "<a href=javascript:history.go ( -1) > This user is currently online and you cannot login to this account from anywhere else! </a> "
Response.End ()
Else
' Otherwise, the decision of landing successfully paid the session
Session ("Lgname") =username
Session ("Lgpass") =userpass
Response.Redirect "Loginok.asp"
Response.End
End If
Else
' If the database doesn't log in to the user record, execute the following statement
Dim ls
Set ls=server.createobject ("ADODB. RECORDSET ")
Ls. Open "SELECT * from Onlylogin", conn_net118,2,2
Ls. ADDNEW
LS ("Olname") =username
LS ("Olip") =olip1
LS ("Oltime") =now ()
Ls. UPDATE
Ls. Close
Set ls=nothing
' Judgment landing successfully paid to session
Session ("Lgname") =username
Session ("Lgpass") =userpass
Response.Redirect "Loginok.asp"
Response.End
End If
End Sub%>
If you can read the ASP file, you will know that the landing after the success of the Leaf would jump to loginok.asp below we immediately look at the code of this leaf
<style type= "Text/css" >
<!--
Body {background-color: #FF9900;}
-->
</style>
<% IF session ("Lgname") <> "" Then%>
You landed a successful!!! Below is a sneak dive into the Web page to refresh the page at the specified time to report to the server whether you are online
For ease of distinction, frame Web pages We use white as the background
<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 landing ha Welcome to Webmaster Information Network: http://www.Net118.com
<% End If%>


If you're a careful person, you'll know right now what I'm doing here is loginframe.asp.
<!--#include file= "loginconn.asp"-->
<% Conn_net118.execute ("Update onlylogin Set oltime= '" & Now () & "Where Olname = '" & Session ("Lgname") &A mp "'")%>

Well, that's it. Our program is completed, the key to the program is to determine whether the user online, and I also have no alternative is to use a frame to set the timing of the refresh to determine the user online key leaf nesting in the main program of the foliage, In the actual operation, you can change the width and height of the iframe to 0 to allow the general user to see, or let the main program's web page background and that nested in the timely refresh of the page just as OK.
Not long ago in the discussion area to see someone said that the session and cookies and other things can also be judged, this is obviously impossible, because they generated objects are working for themselves, their data content can not be shared with other users. Appliaction should be another way to do this, but I thought if there were many users logged in to generate at least one to three Appliaction objects for each user, it would be a shame that our poor server would be dragged down.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.