SoFunction
Updated on 2025-04-09

Introduction to ASP Programming (11): Chat Chat Program

The program used in the usual chat room, that is, 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 on the page between multiple users of the site.

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,


<%If ("Request_Method")="GET" then%>
<form method="post" action="">
<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>
<%
Else

dim talk
If ("nick")<>"" then
Session("nick")=("nick")
End if
%>

<form method="post" action="" 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="">Leave</a><br>

<%
If ("log")<>1 then
If trim(("talk"))="" then
talk=Session("nick")&"I want to give you a perfunctory way without saying a word"
Else
talk=trim(("talk"))
End If

Application("show")="from "&Session("nick")&" said at "&time& "" &talk& "<br>" &Application("show")

Application("show")
End if
%>
<%End if%>



A brief explanation:
1. The function of <%If ("Request_Method")="GET" then%> is to determine the way the current page is accepted. If it is GET, 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"> and the following If ("log")<>1 then are related: obviously, the first time you enter the nickname, the log hidden domain will be sent. But as the first entry, there is no statement to speak, so I judged to accept itThe log value is not 1,that isNot first time login(Indicates that you have logged in), execute the internal related chat display program.

3. trim(("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.


[Ctrl+A Select all to copy Tip: You can modify some code first, then click Run]

4,

Application("show")="from "&Session("nick")&" said at "&time& "" &talk& "<br>" &Application("show")


Extract essence


Application("show")=talk& "<br>" &Application("show")



You can see that it is the overlay function of the Application variable. Each time the value of Application ("show") is based on the original Application variable value, and the latest chat content is attached: the value of the talk variable. This ensures that all users can see the shared information.

Not finished