⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.asp

📁 一个网站聊天室的模型
💻 ASP
字号:
<%@ LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<% 
'设置在服务器输出页面时要先缓冲
response.buffer=true 
%>
<html>
<head>
<title>聊天室</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<%
	dim RoomName,UserID
	'再次保存登录时间,这里是为了防止用户登录成功后,没有直接进入某一聊天室或自行创建聊天室,
	'而在enter.asp页面中停留时间超过5分钟,而在用户想进入聊天室时出现错误
	'在下面更新ursronline表时会体现出它的作用
	LoginTime=now
	
	'创建数据库连接对象
	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"
	'建立到数据源的物理连接,只有使用了Connection对象的Open方法后,到数据源的连接才算真正地建立
	'myconn.Open 
	
	
'如果用户是通过GET方法提交信息的,即从enter.asp页面中通过点击"进入"链接进入聊天室,就...
'这可以与创建新聊天室时通过POST方法提交的用户信息相区别
'也可以防止用没有登录而直接请求该页面
	if Request.ServerVariables ("REQUEST_METHOD")="GET" then
	'Request的QueryString集合取得从enter.asp页面中通过GET方法提交的用户进入的房间的名称和用户的帐号
		RoomName=Request.QueryString ("RoomName")
		UserID=Request.QueryString ("user")
		'定义session变量分别存储用户帐号和房间名
		if session("myname")="" then
		session("myname")=userID
		session("myroom")=RoomName
		'更新在线用户表
		mysql="update UserOnLine set LoginTime='" & LoginTime & "',LastTalkingTime='" & LoginTime & "',RoomName='" & RoomName & "' where OnLineUserID='" & UserID & "'" 
		myconn.Execute (mysql)
		'更新房间信息表
		mysql1="update RoomInfo set HowManyUsers=HowManyUsers+1 where RoomName='" & RoomName & "'"
		myconn.Execute (mysql1)
		'定义application变量保存当前时间
	application("time")=now
	'从在线用户表中查找在该房间中的用户
		newsql="select OnLineUserID from UserOnLine where RoomName='" & RoomName & "'"
		set newrecord=myconn.Execute (newsql)
		'通知所有在该房间中的用户某用户来了
		do while not newrecord.eof
			application(newrecord(0))="<br><font color=purple>" & session("nickname") & "来了!(请刷新)<font color=blue size=1>(" & time()  & ")</font><br><br>" & application(newrecord(0))
			newrecord.movenext
		loop
		
		if application(UserID)="" then
			application(UserID)=application(RoomName)
		end if
	end if'与if session("myname")="" then语句相对应
	
	else'与if Request.ServerVariables ("REQUEST_METHOD")="GET" then语句相对应
	
'如果用户是通过POST方法提交信息的,即从enter.asp页面中通过创建新聊天室而进入聊天室的,就...
'也可以防止用没有登录而直接请求该页面
	if  Request.ServerVariables ("REQUEST_METHOD")="POST" then
		'Request的form集合取得从enter.asp页面中通过POST方法提交的用户新建的房间的名称、用户的帐号和房间的话题
		RoomName=Request.Form ("newroomname")
		UserID=Request.Form ("user")
		Topic=Request.Form ("topic")
		'从在线用户表中查找新建的聊天室的名称是否与已有的房间重名
		'如果重名就提醒用户换一个房间名重新创建或加入已有的房间
		mysql="select RoomName from RoomInfo where RoomName='" & RoomName & "'"
		set myrecord=myconn.Execute (mysql)
		if not myrecord.bof then
		Response.write("此房间名已经存在,请加入该房间!<br>也可选择换一个房间名重新创建新房间!<br>")
		myrecord.close
	    set myrecord=nothing
		Response.End()
		else
		'如果没有与已有的房间重名,就在房间信息表中插入一条新的房间信息
		mysql1="insert into RoomInfo (RoomName,Topic,WhoCreate) values ('" & RoomName & "','" & Topic & "','" & UserID & "')"
		myconn.Execute (mysql1)
		'更新在线用户表
		mysql3="update useronline set RoomName='" & RoomName & "' where onlineuserid='" & UserID & "'"
		myconn.Execute (mysql3)
		'关闭记录集对象
		myrecord.close
	    set myrecord=nothing
	    end if
		end if'与if  Request.ServerVariables ("REQUEST_METHOD")="POST" then语句相对应
		
		'定义session变量分别存储用户帐号和房间名
if session("myname")="" then
		session("myname")=userID
		session("myroom")=RoomName
		'更新在线用户表
		mysql="update UserOnLine set LoginTime='" & LoginTime & "',LastTalkingTime='" & LoginTime & "',RoomName='" & RoomName & "' where OnLineUserID='" & UserID & "'" 
		myconn.Execute (mysql)
		'更新房间信息表
		mysql1="update RoomInfo set HowManyUsers=HowManyUsers+1 where RoomName='" & RoomName & "'"
		myconn.Execute (mysql1)
		
		'通知该房间中的所有用户某用户来了
		newsql="select OnLineUserID from UserOnLine where RoomName='" & RoomName & "'"
		set newrecord=myconn.Execute (newsql)
		
		do while not newrecord.eof
			application(newrecord(0))="<br><font color=purple>" & session("nickname") & "来了!(请刷新)<font color=blue size=1>(" & time()  & ")</font><br><br>" & application(newrecord(0))
			newrecord.movenext
		loop
		
		if application(UserID)="" then
			application(UserID)=application(RoomName)
		end if
	end if
	end if'与if Request.ServerVariables ("REQUEST_METHOD")="GET" then语句相对应
	'关闭数据库连接对象,但在执行set myconn=nothing语句之前该记录集仍然存在
	myconn.Close 
	'彻底关闭数据库连接对象
	set myconn=nothing	

%>
<!--frameset是框架标记-->
<frameset cols="80%,*">
  <!--cols表示横向分隔浏览器窗口,80%表示该框占窗口宽度的百分数,“*”表示将剩下的所有宽度都赋予最后一个框-->
  <frameset rows="75%,*" cols="*">
    <!--rows表示纵向分隔浏览器窗口-->
    <!--下面这个子框架显示content.asp页面-->
    <frame name="ltop" scrolling="auto" noresize src="content.asp?RoomName=<% =RoomName %>&user=<% =UserID %>">
    <!--
  name表示该框窗的名称
  frame表示分隔出的各框栏窗口,
  scrolling="auto"表示根据情况决定是否显示滚动条,
  noresize设定不可以改变这个框架的大小,
  src设定此框窗中要显示的网页文档的名称
  问号?后的内容是通过字符串的形式向content.asp页面传递RoomName和user参数及它们的值
  -->
    <!--下面这个子框架显示send.asp页面-->
    <frame name="lbottom" scrolling="auto" noresize src="send.asp?RoomName=<% =RoomName %>&user=<% =UserID %>">
    <frame src="UntitledFrame-1.htm">
  </frameset>
  <frameset rows="75%,*">
    <frame name="rtop" scrolling="auto" noresize src="currentinfo.asp">
    <frame name="rbottom" scrolling="auto" noresize src="quit.asp">
  </frameset>
  <noframes>
  <!--不支持框架标记,设定当浏览器不支持框架时显示的提示信息-->
  <body>
  <p>这个页面使用了框架,但是你的浏览器不支持框架.</p>
  </body>
  </noframes>
</frameset>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -