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.

PHP Chat Systems A MySQL Driven Chat Script Tutorial

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=="")

{ 0

alert("please enter a value");

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

res= false; 1

}

return res;

} 2

</script>

</head>

<body> 3

<center>

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

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

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

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

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

</form>

</center>

<?php 6

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

if(!($link))

{ 7

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

}

mysql_select_db("roseindia", $link); 8

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

$nick="hello";

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

$nick=$_POST["user"];

while( $row=mysql_fetch_array($result))

{ 0

$rownick= $row['nick'];

if($rownick==$nick)

{ 1

$_SESSION['view']=$nick;

echo $_SESSION['view'];

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

header("location:chatroom.php");

}

} 3

?>

</body>

</html> 4

  chatroom.php

<?php

session_start(); 5

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

$nick=$_SESSION['view'];

$MSG=""; 6

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

$MSG=$_POST['msg'];

?> 7

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

<frame src="msgDisplay.php" />

<frame src="msgInput.php" /> 8

</frameset>

<noframes>

<body> 9

Your browser doesnot support frames

</body>

</noframes> 0

 

msgInput.php

<script type="text/javascript"> 1

function message()

{

result=true; 2

mesg=document.msg.msg.value;

if(mesg=="")

{ 3

alert("Please fill any message");

result= false;

} 4

return result;

}

</script> 5

<?php

session_start();

$user=""; 6

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

$user=$_SESSION["view"];

$MSG=""; 7

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

$MSG=$_POST['msg'];

$link; 8

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

if(!($link))

{ 9

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

}

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

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

?>

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

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

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

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

</form>

msgDisplay.php

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

<?php

session_start();

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

echo $_SESSION['user'];

 

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

if(!$link)

{

die("Error".mysql_error()); 6

}

else

{ 7

mysql_select_db("roseindia",$link);

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

while($row=mysql_fetch_array($query)) 8

{

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

} 9

}

?>

Logout.php 0

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

<?php

session_start(); 1

session_unset();

session_destroy();

?> 2

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

<?php

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

if(!$link)

{

die("Eroor".mysql_error()); 4

}

mysql_select_db("roseindia",$link);

  5

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

?>

  6

Output:

First web page will be look like:

7

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

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

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

9

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: 0

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