PHP Chat Systems A MySQL Driven

PHP Chat Systems A MySQL Driven

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

}

</script>

</head> 1

<body>

<center>

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

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

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

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

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

</form>

</center> 4

<?php

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

if(!($link)) 5

{

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

} 6

mysql_select_db("roseindia", $link);

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

$nick="hello"; 7

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

$nick=$_POST["user"];

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

{

$rownick= $row['nick'];

if($rownick==$nick) 9

{

$_SESSION['view']=$nick;

echo $_SESSION['view']; 0

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

header("location:chatroom.php");

} 1

}

?>

</body> 2

</html>

  chatroom.php

<?php 3

session_start();

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

$nick=$_SESSION['view']; 4

$MSG="";

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

$MSG=$_POST['msg']; 5

?>

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

<frame src="msgDisplay.php" /> 6

<frame src="msgInput.php" />

</frameset>

<noframes> 7

<body>

Your browser doesnot support frames

</body> 8

</noframes>

 

msgInput.php 9

<script type="text/javascript">

function message()

{ 0

result=true;

mesg=document.msg.msg.value;

if(mesg=="") 1

{

alert("Please fill any message");

result= false; 2

}

return result;

} 3

</script>

<?php

session_start(); 4

$user="";

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

$user=$_SESSION["view"]; 5

$MSG="";

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

$MSG=$_POST['msg']; 6

$link;

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

if(!($link)) 7

{

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

} 8

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

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

?> 9

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

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

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

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

</form>

msgDisplay.php 1

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

<?php

session_start(); 2

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

echo $_SESSION['user'];

  3

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

if(!$link)

{ 4

die("Error".mysql_error());

}

else 5

{

mysql_select_db("roseindia",$link);

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

while($row=mysql_fetch_array($query))

{

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

}

}

?> 8

Logout.php

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

<?php 9

session_start();

session_unset();

session_destroy(); 0

?>

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

<?php 1

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

if(!$link)

{ 2

die("Eroor".mysql_error());

}

mysql_select_db("roseindia",$link); 3

 

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

?> 4

 

Output:

First web page will be look like: 5

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

6

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

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

8

After successfully logged out next page will be look like: