Home Php PHP Chat Systems A MySQL Driven Chat Script Tutorial
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

PHP Chat Systems A MySQL Driven Chat Script Tutorial

Advertisement
This article will show you how to create a simple chat script using PHP and MySQL database.

PHP Chat Systems A MySQL Driven Chat Script Tutorial

     

This article will show you how to create a simple chat script using PHP and MySQL database. The application will allow visitors to choose a username, enter message and the user can see the text from other user. One of the most popular use of Internet is communication.

To develop this application we need to create a table which will store nick name (or user name), user id, message text, copy the following text and paste on your mysql editor or console:

create table chat

(userid int(10) unsigned auto_increment, nick varchar(10) not null,text varchar(10) not null, primary key(userid));

After creating the table insert few values:

insert into chat

values (0,'roseindia', 'hello all');

Now roseindia will be the default user who can logged in and chat with someone else, similarly create more users who can chat with each other.

You will come to know about the mechanism of each of the following php files step by step:

To display the messages we need to create a view which will store the last valid records (you can develop your own logic), as follows:

create view msg

as

(select * from chat where userid not in (1,2,3) and text <> '' order by userid desc limit 5);

In the above code select * will fetch each and every value which not comes in 1,2, or 3 (let's consider these values will be reserved for actual users)

, whenever you login an unwanted message as "user: "could generate to prevent from this kind of situation we need to write text<>'', we need to write userid desc limit 5 to display last five messages.

We presume so many things just because of it is just a demo version of chat system, you can develop further. 

      1) chat.php

     

<?php

session_start();

?>

<html>

<head>

<script type="text/javascript">

function nick()

{

var nick=document.chat.user.value;

res=true;

if(nick=="")

{

alert("please enter a value");

//document.tchat.nick.focus();

res= false;

}

return res;

}

</script>

</head>

<body>

<center>

<form name="chat" action="<?php echo $PHP_SELF;?>" method="post" onsubmit="return (nick())">

<input type="text" name="user" ></input>

<input type="hidden" name="action" value="enter"></input>

<input type="hidden" name="chat" value="" ></input>

<input type="submit" value="submit"></input>

</form>

</center>

<?php

$link=mysql_connect("localhost","root","");

if(!($link))

{

die("Can not connect". mysql_error());

}

mysql_select_db("roseindia", $link);

$result=mysql_query("select * from chat");

$nick="hello";

if(isset($_POST["user"]))

$nick=$_POST["user"];

while( $row=mysql_fetch_array($result))

{

$rownick= $row['nick'];

if($rownick==$nick)

{

$_SESSION['view']=$nick;

echo $_SESSION['view'];

//echo "Hello ". $_SESSION['view'];

header("location:chatroom.php");

}

}

?>

</body>

</html>

  chatroom.php

<?php

session_start();

if(isset($_SESSION['view']))

$nick=$_SESSION['view'];

$MSG="";

if(isset($POST['msg']))

$MSG=$_POST['msg'];

?>

<frameset rows="50%,24%">

<frame src="msgDisplay.php" />

<frame src="msgInput.php" />

</frameset>

<noframes>

<body>

Your browser doesnot support frames

</body>

</noframes>

 

msgInput.php

<script type="text/javascript">

function message()

{

result=true;

mesg=document.msg.msg.value;

if(mesg=="")

{

alert("Please fill any message");

result= false;

}

return result;

}

</script>

<?php

session_start();

$user="";

if(isset($_SESSION["view"]))

$user=$_SESSION["view"];

$MSG="";

if(isset($_POST['msg']))

$MSG=$_POST['msg'];

$link;

$link=mysql_connect("localhost","root","");

if(!($link))

{

die("Could not connect".mysql_error());

}

mysql_select_db("roseindia",$link)or die("Can not connect".mysql_error());

$result=mysql_query("insert into chat values (0,'$user','$MSG')");

?>

<form name="msg" method="post" action="<?php $PHP_SELF;?>" onsubmit="return message()">

<input type='text' name='msg'></input>

<input type="submit" value="submit"></input>

<a href="Logout.php" target="_top">Log me out</a>

</form>

msgDisplay.php

<meta HTTP-EQUIV="Refresh" CONTENT="2"></meta>

<?php

session_start();

if(isset($_SESSION['user']))

echo $_SESSION['user'];

 

$link=mysql_connect("localhost","root","");

if(!$link)

{

die("Error".mysql_error());

}

else

{

mysql_select_db("roseindia",$link);

$query=mysql_query("select * from msg order by userid");

while($row=mysql_fetch_array($query))

{

echo $row['nick'].":".$row['text']."<br/>";

}

}

?>

Logout.php

<center><b><u>You are successfully logged out</u></b></center>

<?php

session_start();

session_unset();

session_destroy();

?>

<a href="chat.php"> log me in</a>

<?php

$link=mysql_connect("localhost","root","");

if(!$link)

{

die("Eroor".mysql_error());

}

mysql_select_db("roseindia",$link);

 

mysql_query("delete from chat where userid not in (130,131,132)");

?>

 

Output:

First web page will be look like:

If you forget to type anything, and hit the submit button:

If the username is valid then next web page will look like:

Enter any text and it'll display on the upper frame, here varun is an user:

If you don't enter any text and hit submit button, alert message will display that:

After successfully logged out next page will be look like:

Full code of the chat system is provided along with the following .rar file. The source code of chat application.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 17, 2009

Ask Questions?    Discuss: PHP Chat Systems A MySQL Driven Chat Script Tutorial   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
richa choukade
June 13, 2013
for making chating system in php

tell me about how to make a chating system in php
alebe
December 16, 2011
Hello!

It is too interesting code for exercising web development as a student in any university.
da
December 19, 2011
IT

Everyone
da
December 19, 2011
IT

everyone
pheak
December 19, 2011
IT

Everyone
cadetcaptain
February 1, 2012
nice code

nice tutorial.. thanks..
ravi kumar
July 12, 2012
i cannot create the chat system plz help me

plz help me
DMCA.com