Design an Online Chat Room with PHP and MySQL

Design an Online Chat Room with PHP and MySQLIn this article, you will learn how to design and develop a simple online chat room with PHP and MySQL. This tutorial explains every steps of the development, including both database design and PHP programming. Basic computer skills and knowledge of HTML and PHP are required. Ok, let's begin now.

Step 1: Design Database Table.

Create table "chat" in MySQL database to store basic chat information: chtime (chat time), nick (user nickname) and words (chat message, less than 150 characters)

mysql> CREATE TABLE chat

-> chtime DATATIME,

-> nick CHAR (10) NOT NULL,

-> words CHAR (150);

Step 2: Design Structure.

This simple online chat room includes the following four sections: user login, message display, message input and a main frame integrating the display and input sections. Thus, it needs the following four files to work: login.php, main.php, display.php and speak.php.

Step 3: Write the code

1. login.php (just a HTML form)

<html>
<head>
<title>User Login</title>
</head>
<body>
Please input your nickname and enter
<form action="main.php" method="post" target="_self">
<input type="text" name="nick" cols="20">
<input type="submit" value="login">
</form>
</body>
</html>

2. main.php

<?
setcookie("nick",$nick) //use cookie to store user nickname
?>
<html>
<title>My Chat Room</title>
<frameset rows="80%,*">
<frame src="display.php" name="chatdisplay">
<frame src="speak.php" name="speak">
</frameset>
</html>

3. display.php

This file is used to get message records from database and display the results. To keep the size of database, old messages are deleted and only the newest 15 messages are displayed.

<html>
<head>
<title>Display Messages</title>
<meta http-equiv="refresh" content="5;url=display.php">
</head>
<body>
<?
       //connect to mysql server, server name: main, database username: root
        $link_ID=mysql_connect("main","root");
       mysql_select_db("abc"); //abc is the database name
       $str="select * from chat ORDER BY chtime;" ;
       $result=mysql_query($str, $link_ID);
       $rows=mysql_num_rows($result);
       //get the latest 15 messages
       @mysql_data_seek($resut,$rows-15);
       //if the number of messages<15, get all of the messages
        if ($rows<15) $l=$rows; else $l=15; for ($i=1;$i<=$l; $i++) {
       list($chtime, $nick, $words)=mysql_fetch_row($result);
       echo $chtime; echo " "; echo $nick; echo":" ; echo $words; echo "
";
       } //delete the old messages(only keep the newest 20 only)
       @mysql_data_seek($result,$rows-20);
       list($limtime)=mysql_fetch_row($result);
       $str="DELETE FROM chat WHERE chtime<'$limtime' ;" ;
       $result=mysql_query($str,$link_ID);
       mysql_close($link_ID);
       ?>
</body>
</html>

4. speak.php

<html>
<head>
<title>Speak</title>
</head>
<body>
<?
        If ($words)
        { $link_ID=mysql_connect("main","root");
        mysql_select_db("abc"); // abc is the database name
        $time=date(y).date(m).date(d).date(h).date(i).(date(s); //get current time
        $str="INSERT INTO chat(chtime,nick,words) values ('$time','$nick','$words');" ;
        mysql_query($str,$link_ID); //save message record into database
        mysql_close($link_ID); )
        ?>
        //the following is the message input form
<form action="speak.php" method="post" target=" _self">
<input type="text" name="words" cols="20">
<input type="submit" value="Speak">
</form>
</body>
</html>

Now, you have finished the design and coding of a simple online chat system. Put all the files into your website root and see how it works, :)


Related Articles

  • Online Parties with a Twist
    We know once you have a room full of guests at your online party, getting them to play games is the easy part. While hunting for answers to your game, a guest has the chance to see some products they simply cannot do without, thus making you sales!...
  • Five Proven Ways To Promote Your Web Site
    1. Holding a contest or sweepstakes is an proven way to promote your web site. You can announce your site to hundreds of web sites that list free contests and sweepstakes. Send out a press releases about your contest or sweepstakes...
  • 10 Tips How To Skyrocket Your Sales
    1. End your slow sales periods by planning ahead. Plan to add extra bonuses, hold a sale or package your product with other products. For example, you could say, Buy one, get one free! Another example would be, Buy over $20 worth of products and get 5 free bonuses!
  • Strategies to Keep Your Customers Visiting Your Website
    Just like in the real world retaining your customers is a key to business success. The more visits your site gets the more chances are that someone will buy your product or services. As a result its important that your provide tools and strategies that will retain your customers, keep them coming ba...
  • MySQL Join Tutorial
    What are joins and how do you make them?
  • Acquiring PHP MySQL
    PHP and MySQL are already packaged in Linux and Mac OS X OSs. However, most PHP developer are actually using Windows when developing PHP applications. This is also true to myself. I have Mandrake Linux 10 but Ill just use it for testing purposes of my finished scripts...
  • Alternating Rows: Mysql
    The first thing we need to do is connect to the database. I always put my db connection in a universal file, that way I can just change the variables once if I need to; but for the sake of the tutorial, im putting it in this file.
  • PHP Navigation and Connecting to a MySQL Database
    PHP Navigation and Connecting to a MySQL Database
  • Online Press Rooms Save the Media Time and Frustration
    When Web site usability guru Jakob Nielsen tested how well major corporate sites met the needs of reporters, he gave them a D grade. Jo...
  • Alternating Row Colors
    Theres two different ways this can be done. The first is by getting results from a mysql query, the second is going through the values of an array. Ill go over both. First the full files (ill break it up into two files, one using mysql, and one using an array)

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.