Use Php+mysql to build a chat room

Source: Internet
Author: User
Tags html page mysql in

MySQL concurrency is strong, fast response, excellent performance of the database software; PHP is a powerful server-side scripting language. The author in Shanxi aluminum Plant website development, uses php4.0+mysql3.23.38 to establish many kinds of applications. Below, take a simple chat room design as an example, introduce the application of Php+mysql in Web page development.

1. Overall design

1.1 Conception and Planning:

The basic principle of the chat room is to store the speech data of each user who is connected to the same Web page, and then pass all the speech data to each user. That is, using a database to assemble everyone's speeches, and pass the data in the database to everyone, realizes the function of the chat room.

1.2 Table Design

First use MySQL to create a table chat to store users ' speeches:

mysql> CREATE TABLE Chat

-> (Chtime datatime,

-> Nick CHAR (Ten) not NULL,

->words CHAR (150));

The table only sets three domains, Chtime is the time to speak, Nick is the speaker's nickname, words is the content of the speech, speak up to 150 characters

1.3 Web Design

One of the simplest chat rooms usually requires two page boxes: a page box is a form where the user enters the statement, and the other is used to show the speaker. So code snippets usually require at least the following paragraphs:

Build the structure of the page frame (main.php)

section of the program showing members ' statements (cdisplay.php)

Segment of the program transmitting user statements (speak.php)

User Login into Chat room program section (login.php)

2. Code Design

After the completion of the above plan, you can start code design, PHP can be very concise to achieve the above functions.

2.1 User Login login.php, this code is a full HTML page

<html>
<head>
 <title>用户登录</title>
</head>
<body>请输入您的昵称<br>
<form action=”main.php” method=”post” target=”_self”>
 <input type=”text” name=”nick” cols=”20”>
 <input type=”submit” value=”登录”>
</body>
</html>

After the user submits his nickname, he enters into the chat room, and the following processing is referred to main.php for processing.

2.2 Page Frame body code segment main.php:

<?
 setcookie(“nick”,$nick) //用cookie记录用户昵称,是常用的传递变量方法
?>
<html>
<title>山西铝厂聊天室试用版ver1.0</title>
<frameset rows=”80%,*”>
<frame src=” cdisplay.php” name=”chatdisplay”>
<frame src=”speak.php” name=”speak”>
</frameset>
</html>
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: 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.