The program used in the usual chat room, namely the Chat program, does not use the database for its basic structural principle. So what technology is used? We know that the function of the Session variable in ASP variable is to record the information of a single user and can track the behavior of the user; the function of the Application object can enable the sharing of information between multiple users of the site on the page.
It can be imagined that in the current chat program, a chat member is a Session variable, and the conversations between chat members are shared and displayed as Application variables so that each member can see them.
Then, let’s use a very classic example program to understand and analyze.
1, chat.asp
<%If Request.ServerVariables("Request_Method")="GET" then%> <form method="post" action="chat.asp"> <input type="text" name="nick" value="your nick name"><p> <input type="submit" value="come in"><p> <input type="hidden" name="log" size="20" value="1"> </form> <%Response.End Else Response.clear dim talk If Request.Form("nick")<>"" then Session("nick")=Request.Form("nick") End if %> <form method="post" action="chat.asp" name=form1> <%=Session("nick")%>Speak: <input type="text" name="talk" size="50"><br> <input type="submit" value="submit"> <input type="reset" value="Cancel"></p> </form> <a href="chat.asp">Leave</a><br> <% If Request.Form("log")<>1 then If trim(Request.Form("talk"))="" then talk=Session("nick")&"I want to give you a perfunctory way without saying a word" Else talk=trim(Request.Form("talk")) End If Application.lock Application("show")="from "&Request.ServerVariables("remote_addr")& "" &Session("nick")&" said at "&time& "" &talk& "<br>" &Application("show") Application.UnLock Response.Write Application("show") End if %> <%End if%> |
A brief explanation:
1. The function of <%If Request.ServerVariables("Request_Method")="GET" then%> is to determine the way the current page is accepted. If it is a GET method, the form page "requires nickname" will be displayed. Because the silent acceptance method of the page is GET, when you type in the URL address bar directly, that is, when there is no information, you should display the requirement to "enter a nickname".
2. <input type="hidden" name="log" size="20" value="1"> is associated with the following If Request.Form("log")<>1 then: obviously, the first time you enter the nickname, the log hidden domain will be sent. However, as the first entry, there is no statement to speak, so when it is judged that the accepted log value is not 1, that is, it is not the first time login (which means that it has been logged in), the internal related chat display program will be executed.
3. trim(Request.Form("talk"))="", trim is a function: delete the spaces before and after the string. At first, there is also rtrim(): remove the spaces after the string; ltrim(): remove the spaces before the string.