php_3_ "Simple chat room" Implementation of key technical details

Source: Internet
Author: User

php+mysql Key Technologies for implementing a simple chat room on the internet

System target:

The chat room uses the database to assemble each person's speech, and the information in the database can be displayed on the page so that each user can see the following features:

A. User login: Display their login information when the user speaks

B. User statements: User input words

C. Display of statements: Users view all spoken information

Design ideas:

(1). Set up the chat room database and related data sheet

(2). Implement User Login page (login.php)

(3). Implementation Statement page (speak.php)

(4). Implementation Statement Display page (chat_display.php)

(5). Implementing the Main Page (main.php)

Key technologies:

1. Automatically refresh the page:

If you want to make the page automatically refresh at a certain time, it is implemented by adding the following statement between the <HEAD> and </HEAD> tags of the HTML:

<meta http-equiv= "Refresh" content= "number of seconds"; url= jump file or address >

For example: When the user comes to page 20s automatically jumps to another page new.php,

<meta http-equiv= "Refresh" content= ";url=new.php>"

2. Pass the user name:

After the user logs in to the chat room, the system should keep its login information so that it can be used in the speech, through the session to achieve. session is a commonly used information storage mechanism , which is to illustrate the application in this example.

(1). When a user logs on to the system, it is necessary to register a session variable to store its logon name information:

1 <? PHP2         session_start ();                              // load session library, must be placed in the first line $user _name=$_post["Nick";  Register $user _name variable 5?>          

The 2nd line opens the session function, the 3rd line gets the nickname information entered by the user on the page form, and the 4th line registers the session variable user_name.

(2). On the user's statement page, when speaking, get the session variable user_name and put its value in the database together with the statement:

Session_Start();· $author =$_session["user_name"];  Deposit author information into the database 5?>       

This way, the system will always use the session to save the login information before the user leaves the system, which can be used whenever needed.

3. Show the latest statements:

Simple chat room Another problem that needs to be solved is that when you display the message, you can't show it all because you have a lot of speech information, but only the most recent number of statements. This can be done by sorting the statements in chronological order, and selecting the final part of the statement.

Here is the latest 15-statement message on the page:

1 @Mysql_data_seek ($resut,$rows-15);//Move the record pointer to the first 15 records2if ($rows <15)3$l =$rows;4Else5$l = 15;//The total number of records is less than 15, which is the maximum number of records6for ($i = 1;$i <=$l;$i + +)//To iterate through the speech message7{8List$cid,$author,$create _time,$text) =Mysql_fetch_row ($result); 9 echo  $create _time< Span style= "color: #000000;" >; 10 echo "" ;11 echo "" ". ; 12 echo "said:"  13 echo  $text 14 echo "<BR>"; // output message content 15}   

Where the 1th line $result to query all the messages obtained from the recordset, through the Mysql_data_seek () command to move its record cursor to the 15th record, and then the 6~15 line output up to 15 statements.

In addition, the database does not need to keep all user speech information, or it will be the database speed expansion. You can use one strategy: keep only a certain number of recent speeches, and delete the other overdue statements in a timely manner.

System implementation:

(1). Create a database:

CREATE DATABASE ' my_chat ';

(2). Create a chat table:

1 -- creating chat table:2createtable ' chat ' (3NULL auto_increment,4  null ,5timenull ,6NULL ,7KEY 8);  

php_3_ "Simple chat room" Implementation of key technical details

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.