📄 enter.asp
字号:
<%@ LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--
@和Language之间必须要有一个空格;
Language=VBScript用来说明页面使用的脚本语言是VBScript;
CODEPAGE="936"是用来设置页面的代码页(字符编码)
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--声明HTML语言的版本信息-->
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<BODY>
<p>
<%
'验证index.asp页面提交的信息
'如果表单是用POST方法提交的,就验证用户登录信息
if Request.ServerVariables ("REQUEST_METHOD")="POST" then
'这个条件也限制了未登录成功而直接请求该页面的用户将不会看到任何内容
'只有通过index.asp页面的表单提交信息,并且登录成功,才会看到聊天室的现有信息,
'并提供进入聊天室的链接入口(如果房间没有被锁住的话)
'定义变量分别存储用户的帐号、密码、昵称、IP地址和登录时间
dim UserID,UserPwd,NickName,IP,LoginTime
'定义变量分别用来存放不同的SQL语句
dim mysql0,mysql1,mysql2,mysql3,mysql4
'使用request的form集合来取得从index.asp页面的表单中提交的数据信息,分别赋给各变量
'使用变量保存用户帐号
UserID=Request.Form ("UID")
'使用变量保存用户密码
UserPwd=Request.Form ("PWD")
'使用变量保存用户昵称
NickName=Request.Form ("NickName")
session("nickname")=NickName'定义一个保存用户昵称的session变量
''使用变量保存用户登录时间,这里是把当前时间保存为用户登录时间
LoginTime=now()'now是函数,返回当前的日期和时间
'使用变量保存用户的IP地址
'这里是用Request的ServerVariables集合取得用户的IP地址
'Remote_HOST是返回发送请求的远程主机名的关键字
IP=Request.ServerVariables("Remote_HOST")
'以下3行代码行(注释行除外)是使用字符串方式建立到数据库连接
'创建数据库连接对象
set myconn=server.CreateObject ("ADODB.Connection")
'使用server对象的mappath方法取得数据库的存放路径
dbpath=server.mappath("chatroom.mdb")
'建立到数据源的物理连接,只有使用了Connection对象的Open方法后,到数据源的连接才算真正地建立
'这里是使用字符串的方法创建到数据源的连接,它相对于使用ODBC方法的好处是可以不受相对路径的限制
myconn.open "driver={Microsoft Access Driver (*.mdb)};dbq="&dbpath
'如果是使用ODBC连接数据库,就应该使用下面的3行代码替代上面使用字符串连接数据库的3行代码(注释行出外)
'创建数据库连接对象
'set myconn=server.CreateObject ("ADODB.Connection")
'建立连接数据源的信息
'myconn.ConnectionString ="DSN=ChatRoom"
'无论在等号 (=) 之前还是之后,DSN 字符串都不能包含空格
'建立到数据源的物理连接,只有使用了Connection对象的Open方法后,到数据源的连接才算真正地建立
'myconn.Open
'判断index.asp页面提交的用户名和密码是否正确
'从已注册用户表user中查找用户的帐号和密码
mysql0="select UserID,PWD from User where UserID='" & UserID & "' and PWD='" & UserPwd & "'"
'通过connection对象的execute方法执行SQL语句并返回记录集
set myrecord0=myconn.Execute(mysql0)
'如果没有记录,即没有注册过,就...
if myrecord0.eof then
'向浏览器输出信息
Response.Write ("密码或帐号输入错误!<a href='index.asp'>请重新登录</a>")
Response.Write ("<p>如果您还没有注册,请先<a href='NewUser.asp'>注册</a></p>")
'终止当前ASP页面的执行,即其后的代码都不执行
Response.End
else
'判断该帐号的用户是否已经登录在线
mysql1="select * from UserOnLine where OnLineUserID='" & UserID & "'"
'通过connection对象的execute方法执行SQL语句并返回记录集
set myrecord1=myconn.Execute (mysql1)
'如果该用户已经在线,就...
if not myrecord1.bof then
'如果用户刚刚登录过,而且该用户最后发言时间与现在的时间间隔不超过5分钟,就通知用户5分钟后再登录
if dateadd("n",5,myrecord1("LastTalkingTime"))>now then
Response.Write ("你已经登录过了,请在五分钟后再登录!")
'关闭记录集对象
myrecord1.close
set myrecord1=nothing
'终止当前ASP页面的执行,即其后的代码都不执行
Response.End
else
'如果该用户最后发言时间与现在的时间间隔超过5分钟,就从在线用户表中删除该用户
newsql="delete from UserOnLine where OnLineUserID='" & UserID & "'"
myconn.Execute (newsql)
end if
end if
'关闭记录集对象
myrecord1.close
set myrecord1=nothing
'判断用户帐号是否被封
'判断该用户是否有被封的记录,如果有,就判断现在是否已经被解封
mysql2="select IDIfClosed,IDOpenTime from Fault where FaultUserID='" & UserID & "'"
set myrecord2=myconn.Execute (mysql2)
if not myrecord2.bof then
if myrecord2("IDIfClosed")=true then
if myrecord2("IDOpenTime")<date() then
Response.Write ("你的帐号刚刚被解封,请<a href='index.asp'>重新登录</a>,以后请注意!")
newsql="update Fault set IDIfClosed=false where FaultUserID='" & UserID & "'"
myconn.Execute (newsql)
Response.End
else
Response.Write ("你的帐号已经被封,到" & myrecord2("IDOpenTime") & "才开放!")
Response.End
end if
end if
end if
myrecord2.close
set myrecord2=nothing
'判断IP地址是否被封
mysql6="select IPIfClosed,IPOpenTime from Fault where FaultIP='" & IP & "'"
set myrecord4=myconn.Execute (mysql6)
'判断该用户是否有被封的记录,如果有判断现在是否已经被解封
if not myrecord4.bof then
if myrecord4("IPIfClosed")=true then
if myrecord4("IPOpenTime")<date() then
Response.Write ("你的IP地址刚刚被解封,请<a href='index.asp'>重新登录</a>,以后请注意!")
newsql="update Fault set IPIfClosed=false where FaultUserID='" & UserID & "'"
myconn.Execute (newsql)
Response.End
else
Response.Write ("你的IP地址已经被封,到" & myrecord4("IPOpenTime") & "才开放!")
Response.End
end if
end if
end if
myrecord4.close
set myrecord4=nothing
'通过了所有的验证条件,即帐号和密码都正确,没有登录过,而且ID,IP都没有被封
'就列出现有聊天室的信息供用户选择房间进入
'同时也显示“新建聊天室”的链接,供不想进入现有房间的用户创建新的聊天室
'定义一个session变量
session("login")="ok"
'定义一个标志用户登录成功的session变量,用于记录用户登录成功
'该session变量主要用于在通过enter.asp页面中的新建聊天室超链接连接newcreate.asp页面时的验证
'如果用户已经登录成功,即session("login")="ok",就显示newcreate.asp页面的内容
'如果没有登录成功,即session("login")变量不等于"ok",请求newcreate.asp页面时就不会显示任何内容
'定义一个application变量,记录当前时间
application("time")=now
%>
<!--"新建聊天室"链接,链接到创建新聊天室的页面-->
<div align="right"><b><font color="#FF6600" size="4"><a href=newcreate.asp?user=<% =UserID %>>新建聊天室</a></font></b> </div>
<!--
<b>是粗体标记
新建聊天室链接中?后的内容是通过字符串的形式向newcreate.asp页面传递user参数及它的值
在newcreate.asp页面中是通过GET方法来获取该参数的值的
-->
<%
if session("refresh")="" then '定义一个session变量
'更新用户表user
mysql3="update User set VisitCount=VisitCount+1 where UserID='" & UserID & "'"
'从在线用户表useronline中插入该用户的信息
mysql4="insert into UserOnLine (OnLineUserID,NickName,OnLineUserIP,LoginTime,lasttalkingtime) values ('" & UserID & "','" & NickName & "','" & IP & "','" & LoginTime & "','" & LoginTime & "')"
'执行SQL语句
myconn.Execute (mysql3)
myconn.Execute (mysql4)
session("refresh")=true'为session变量赋值
end if
'查询并列出所有房间信息
mysql5= "select * from RoomInfo"
set myrecord3=myconn.Execute (mysql5)
%>
</p>
<!--水平线标记,宽度为4-->
<hr size="4">
<!--以下部分是用表格列举出当前房间信息-->
<table width="811" height="48" border="1" align="center">
<tr>
<td width="18%" align="center"><b><font face="楷体_GB2312">房间名</font></b></td>
<td width="17%" align="center"><b><font face="楷体_GB2312">话题</font></b></td>
<td width="12%" align="center"><b><font face="楷体_GB2312">当前人数</font></b></td>
<td width="13%" align="center"><b><font face="楷体_GB2312">创建者</font></b></td>
<td width="19%" align="center"><b><font face="楷体_GB2312">是否上锁</font></b></td>
</tr>
<%
'列出所有房间信息
do while not myrecord3.eof
%>
<tr>
<%
'判断该房间是否上锁了,如果该房间上锁了,就判断该房间的创建者是否在还在该房间中,如果创建者已经不在该房间中,就把该房间的锁打开
if myrecord3("IfLocked")=true then
newsql="select OnLineUserID from UserOnLine where RoomName='" & myrecord3("RoomName") & "' and OnLineUserID='" & myrecord3("WhoCreate") & "'"
set myrecord=myconn.Execute(newsql)
'定义一个标志变量,用该标志变量记录房间创建者已经离开房间,且该房间的锁已经打开
dim flag
'如果房间创建者已经离开了该房间,就把该房间的锁打开
if myrecord.bof then
newsql="update RoomInfo set IfLocked=false where RoomName='" & myrecord3("RoomName") & "'"
myconn.Execute (newsql)
flag=1
end if
'关闭记录集对象
myrecord.close
set myrecord=nothing
end if
'如果房间没有上锁或房间创建者已经离开房间,且该房间的锁已经打开,就...
if myrecord3("IfLocked")=false or flag=1 then
%>
<!--如果该房间没有上锁或已经解锁,就设置一个"进入"链接,通过它可以直接进入房间-->
<td width="18%" align="center"><b><font face="楷体_GB2312">
<% =myrecord3("RoomName") %>
</font></b> <FONT color=maroon size=2><a href=main.asp?RoomName=<% =myrecord3("RoomName") %>&user=<% =UserID %>>进入</a></FONT></td>
<!--main.asp?后的内容是通过字符串的方法向main.asp页面传递房间名和用户帐号参数及其值-->
<!--在main.asp页面中是通过GET方法来获取该参数的值的-->
<%
else
%>
<!--如果该房间已经被锁住了,就显示"锁住",并且没有链接可以进入该房间-->
<td width="17%" align="center"><b><font face="楷体_GB2312">
<% =myrecord3("RoomName") %>
</font></b> <FONT color=maroon size=2>锁住</a></FONT></td>
<%
end if
%>
<!--房间话题-->
<td width="12%" align="center"><b><font face="楷体_GB2312">
<% =myrecord3("Topic") %>
</font></b></td>
<!--人数-->
<td width="13%" align="center"><b><font face="楷体_GB2312">
<% =myrecord3("HowManyUsers") %>
</font></b></td>
<!--创建者-->
<td width="19%" align="center"><b><font face="楷体_GB2312">
<% =myrecord3("WhoCreate") %>
</font></b></td>
<!--是否上锁-->
<td width="12%" align="center"><b><font face="楷体_GB2312">
<% =myrecord3("IfLocked") %>
</font></b></td>
</tr>
<%
'将记录指针移到下一条记录
myrecord3.MoveNext
loop
%>
</table>
<p>
<%
end if'与开头的第二个if语句"if myrecord0.eof then"语句相对应
'关闭记录集对象,但在执行set myrecord0=nothing语句之前该记录集仍然存在
myrecord0.close
'彻底关闭记录集对象
set myrecord0=nothing
myrecord3.close
set myrecord3=nothing
'关闭数据库连接对象,但在执行set myconn=nothing语句之前该记录集仍然存在
myconn.close
'彻底关闭数据库连接对象
set myconn=nothing
end if'与if Request.ServerVariables ("REQUEST_METHOD")="POST" then语句相对应
%>
</P>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -