ASP programming BASICS (11): Chat program

Source: Internet
Author: User
Tags servervariables

The common Chat room program, that is, the Chat program, does not use the basic structure of the database. So what technology is used? We know that the Session variable in ASP variables is used to record the information of a single user and track user behavior. The Application object acts as a global variable, multiple users on the site can share information on the page.

As you can imagine, in the current chat program, a chat member is a Session variable, and sessions between chat members are shared and displayed as Application variables so that all Members can see them.

Next we will use a classic instance program for understanding and analysis.

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">
<% Response. End
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") %> speaker:
<Input type = "text" name = "talk" size = "50"> <br>
<Input type = "submit" value = "submit">
<Input type = "reset" value = "cancel"> </p>
<A href = "chat. asp"> exit </a> <br>

If Request. Form ("log") <> 1 then
If trim (Request. Form ("talk") = "" then
Talk = Session ("nick") & "don't say a word, you just want to give it away"
Talk = trim (Request. Form ("talk "))
End If
Application. lock
Application ("show") = "from" & Request. serverVariables ("remote_addr") & "& Session (" nick ") &" in "& time: "& talk &" <br> "& Application (" show ")
Application. UnLock
Response. Write Application ("show ")
End if
<% End if %>

Simple explanation:
1, <% If Request. serverVariables ("Request_Method") = "GET" then %> is used to determine how the current page is accepted. If the GET method is used, a single table page with "Enter nickname required" is displayed. Because the silent acceptance method of the page is GET, when the URL address bar is directly typed, that is, when there is no information, it should be displayed to require "input nickname ".

2, <input type = "hidden" name = "log" size = "20" value = "1"> and the following If Request. form ("log") <> 1 then is associated: Apparently, the first time a nickname is entered, the log hidden domain is also sent. However, as the first entry, there is no statement to speak, so the decision is accepted.The log value is not 1., That isNon-First Login(Indicates that you have logged on), execute the internal chat display program.

3, trim (Request. Form ("talk") = "", trim is a function: the space before and after the string is deleted. At the beginning, there is also rtrim (): remove the space after the string; ltrim (): remove the space before the string.

<Script language = vbs> <br/> cnbruce = "This is a Test! "<Br/> alert (" show all "& cnbruce) <br/> alert (" delete leading space: "& ltrim (cnbruce )) <br/> alert ("Remove trailing space:" & rtrim (cnbruce) <br/> alert ("Remove trailing space:" & trim (cnbruce )) <br/> </script> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]


Application. lock
Application ("show") = "from" & Request. serverVariables ("remote_addr") & "& Session (" nick ") &" in "& time: "& talk &" <br> "& Application (" show ")
Application. UnLock

Extract the essence

Application. lock
Application ("show") = talk & "<br>" & Application ("show ")
Application. UnLock

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


Related Article

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: 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.