PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Design an Online Chat Room with PHP and MySQL

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="/img_articles/8028/display.php" name="chatdisplay">
<frame src="/img_articles/8028/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, :)



About the Author:

Rory Canyon is the founder of ScriptMenu.com, a free web directory for PHP, ASP, ASP.NET, PERL, XML, Java, JavaScript, Flash, CFML, Python and other web programming resources.

For more information, visit scriptmenu.com

Author's URL: Rory Canyon
PHP is open source scripting language. It\'s widely used to develop web applications. More PHP Tutorials: Featured Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Reader's comments
comments Hurst32Lacy July 16, 2011 says:
Set your own life time more simple take the <a href="> and everything you require.
Reply
comments Dragonflai February 27, 2011 says:
great script, but seem doesn't work...
I start to login, then the main page is uploaded.. ok.
i write inside speak..
but nothing is displayed inside the chat page.

Database in empty... i have this structure:
db name: chat12
table chat : 3 field:
chtime datetime No None
nick char(10) llatin1_swedish_ci No None
words char(150) latin1_swedish_ci No None

main is changed with localhost and root is root..
why? :-/
thank you in advance

Reply
comments jijo November 17, 2010 says:
hi,
that,s nice. but there is nithing displayed on my screen

Reply
comments tldmic November 23, 2009 says:
Hi Roy,
this article is great,but the problem comes when loading the files you have specified on main.php,can you please help on that?
michael

Reply
comments Jacking August 16, 2010 says:
on main.php change this code
<frame src="/img_articles/8028/display.php" name="chatdisplay">
<frame src="/img_articles/8028/speak.php" name="speak">
into
<frame src="display.php" name="chatdisplay">elp
<frame src="speak.php" name="speak">
hope help your problem...

Reply
Add comments to "Design an Online Chat Room with PHP and MySQL"

Captcha