Chat Room Construction Detailed a

Source: Internet
Author: User
Tags functions insert query variable access database client
A master programmer, in each development of a project, often to go through very strict planning, very strict verification, otherwise, the painstaking development of the program once found problems, it is often naught, from the beginning. Especially in developing a large project, the plan and verification process is more important than the programming process.

However, if we are a beginner of programming, let us develop a routine that is often the opposite of what we have done. Often starting from the simplest example, to achieve the most preliminary functions, and then on the basis of a simple process to improve and improve, so that its functions continue to increase, development, strong.

I myself have a habit: in the design of some small and medium-sized programs, I like to spend more energy to explore, to find the difficult part of it, mining its core, and then from its core to start experimental programming. Usually a program has a few difficult points and a core part. Once these core parts have been completed, we can plan for the entire program, so that the design can be completed more quickly.

We want to build a chat room, first of all, the overall profile of this chat room in our minds to have a preliminary image, in many public chat rooms, its core is:
1, the chat to write the statement text or database;
2, the browser constantly to the text or database to refresh, read;
3, real-time updating of online customers;
4, the administrator of the chat to play or IP prohibit operation.

If a beginner program design, you can achieve the writing database, automatic refresh, access to customer IP experiments, then, in fact, the construction of the chat room also completed the half. First of all, to complete these experiments, we should at least familiarize ourselves with some ASP scripts and some SQL commands.

1, <%ip=request ("REMOTE_ADDR")%>
Your IP address is:
<%=IP%>

This is a script to get client IP, if you read the ASP tutorial, you should know that REMOTE_ADDR is
A server environment variable that returns the IP address of the remote host (client) that issued the request.

2, <%
Set conn=server.createobject ("ADODB. Connection ")
Connstr= "dbq=" +server.mappath ("Chat.mdb") + ";D river={microsoft Access DRIVER (*.mdb)};"

Conn.Open ConnStr
Sql= "SELECT * from online user table WHERE name = '" & Request ("name") & ""
Set Rs=conn. Execute (SQL)

If rs.bof OR rs.eof Then
SZ = "'" & Request ("name") &_
"', '" & Request ("D4") &_
"', '" & Time1 & ""
into_db = "INSERT into online user table (name, sex, login time) VALUES (" &_
SZ & ")"
Conn. Execute (into_db)
End If
%>

This is a very common script for database operations, which includes conditional queries and record additions to the database. The above procedure is divided into three parts, such as three parts separated by a space.
To use the object provided by the component, create an instance of the object and assign the new instance to the variable name. Using the ASP's Server.CreateObject method to create an instance of an object, a variable assignment instruction using a scripting language can be named for an object instance, as in the following example:
Set conn=server.createobject ("ADODB. Connection ")
The variable conn Here is an instance of an object that the ASP program creates to access the database.
To access the database, we must first know where the database is, what is the driver for this database, there are two ways to specify these two points: the first is to manually create the database source name on the server side, specify the database path, specify the database driver, this method is through the server side of the control surface version of the ODBC To set the. Her advantage is that you can limit access to the database, increase the security of the database, the disadvantage is that the database is not easy to transplant.
Another approach is to use ASP to specify the database path and the database driver. You do not have to manually create a database source. In this case, we are using the second method. Server.MapPath ("Chat.mdb") specifies the database path, Driver={microsoft access DRIVER (*.mdb)}, and specifies the database driver, in this case we are using the MS Access database.

The select is a SQL query command. Sql= "SELECT * from online user table WHERE name = '" & Request ("name") & "" indicates that all the names in the online user table in the query database Chat.mdb are equal to Request ("name"), Req Uest ("name") reads data in the form named name.

Bof is the beginning of the file, and EOF is the end of the file. If rs.bof OR rs.eof Then ... Statements are often used to determine whether a qualifying record exists in the database, if the record does not exist, or if it is a false record.
Insert INTO is an insert command in SQL that is often used to insert a record.

The above procedure, the whole meaning is: Query the database online user table whether there is a user request ("name"), if not present the user to add the user to the Online user table.

3, <meta http-equiv= "Refresh" content= "4" >
This is the simplest page automatic refresh script, and she is an HTML script, not an ASP script. Quite simply, it means to refresh every 4 seconds, but in the course of use, you will find that there are many problems appearing.


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: info-contact@alibabacloud.com 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.