Index >> About Us >> Add Url >> Privacy Policy >> Terms of Service >> Add Article
Search:   
keenvisitor.com keenvisitor.com
 
 

Having A Website And Not Using AdSense?

I received an email from a subscriber telling me he is promoting affiliate products on his website a ... - Jean Lam
 

Email Marketing Essentials

Perhaps the most important component in turning visitors to your web site into customers is email ma ... - Kevin William Davies
 

Toner Cartridges: Should You Use Remanufactured or Compatibles?

Toner cartridges come in a variety of flavours now under the banner of original and non-original wit ... - Andrew F James
 

Subcontracting your SEO and Web Development

Web development for the SEO (search engine optimization) expert includes using copywriters to genera ... - Mark Nenadic
 

Electronic Payment Processing

Electronic payments only came into existence about a decade ago when the Internet became widespread. ... - Jimmy Sturo
 

PC Recycling Programs

PC recycling used to be easy. But now, since most people have computers, and schools and charities a ... - Joshua Feinberg
 
 

  Index » Computers & Networking » Website Development
   
 

Chat Room Design with PHP and MySQL

   
Author: Rory Canyons
 

In 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

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 "

<BR>";

} //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

 
Internet Directories and Back Links
 
Mobile Phones To Help Fight Crime
 
Wan Optimization Know How
 
So You Want to Save Outlook Messages in SharePoint
 
Domain Name Registration Process - Basic Guide
 
Make your mobile phone stylistic with accessories
 
Best Internet Affiliate: How You Can Get There With Articles Marketing
 
Keywords - Key For All Doors
 
Spyware Protection Software
 
Buying Wholesale Web Site Traffic
 
 
 
Add Url
 

People & Society

Sports & Adventure

Relationship & Lifestyle

Events & News

Automobile & Automotive

Hotels & Travel

Computers & Networking

Malls & Shopping

Culture & Art

Science & Space

Eating & Drinking

Business & Commerce

Finance & Banking

Children

Hygiene & Health

Recreation & Entertainment

Academics & Education

Government & Politics

Healthcare & Medicine

Garden & Home

Self Healing

Careers & Employment

Games & Play

Estate & Realty

 
Index >> Privacy Policy >> Terms of Service  
Copyright © 2006-2008 www.keenvisitor.com - All Rights Reserved.