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


 
 

Share on Google+Share on Google+

PHP Chat Systems A MySQL Driven

Advertisement
PHP Chat Systems A MySQL Driven

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:

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: April 18, 2011

Ask Questions?    Discuss: PHP Chat Systems A MySQL Driven  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
get_slayeer
August 19, 2011
awsome tutorial

thankxxx alot its very helpfull for me,,m a bigener developer of php ajax js sort of languages ,,please keep up to dating such tutorials that is benifitable for beginners liek me:)
ww
September 2, 2011
www

www
Naim Rajib
September 26, 2011
Error in Chat

When I start chat using your script it shows following message : "Cannot send session cache limiter - headers already sent" in your code you start seesion all the php file.when i remove all except chat.php it works but mot show nick name in last text and message input box is default activate.it requries again to go that text box.i use to remove this warning "ob_start()".but it is not work.please suggest me what i should done. thanks for this type of script.
krishna
March 1, 2012
php

thank u buddy,,,, it worked a lot for me.
soha
March 22, 2012
query

i want to make t5his ,please tell me where i will write php code,please describe3 me one by one with reasons.thanks 2 help me.
sujoy
April 13, 2012
good

nice
DMCA.com