bookmark - Simple Chat System Create your own basic chat system using PHP!

Simple Chat System - Create your own basic chat system using PHP!

 
 Discussion by 8ennett with 6 Replies.
 Last Update: February 3, 2010, 8:32 am
 
bookmark - Simple Chat System Create your own basic chat system using PHP!  
    
free web hosting
 
This tutorial will demonstrate how to create a chat application for your website using PHP and MySQL . The application will allow the user to select a username and to enter messages that will be displayed for another user to read.

First of all you will need to create a MySQL table which will store the username, user id, and the messages body. Copy the following MySQL query and paste it in your MySQL editor or console:

CREATE TABLE IF NOT EXISTS `chat` (
`userid` int(10) UNSIGNED auto_increment,
`nick` varchar(10) NOT NULL,
`text` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

After creating run the next MySQL query to insert our initial values as a test:

INSERT INTO `chat` (`userid`, `nick`, 'text') VALUES
(0, 'Bennett', 'Hello World');

Now Bennett is the default user who can log in and send more messages. You can also use this method to create more users.

I will run you through how each of the following PHP files work::

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 are assuming several things here as this is just a demo of the chat system. You can later develop these files to suit your own needs. Now create a PHP document called 'chat.php' and copy and paste the following code in to it:

CODE

<?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("Bennett", $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>


Next we want to create a new PHP document called chatroom.php and copy and paste the following code in to it:

CODE

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


Next create a PHP document called msgInput.php and copy and paste the following code:

CODE

<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("Bennett",$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>


Now create another PHP document called msgDisplay.php and copy and paste the following:

CODE

<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("Bennett",$link);
$query=mysql_query("select * from msg order by userid");
while($row=mysql_fetch_array($query)){
echo $row['nick'].":".$row['text']."<br/>"; } }
?>


Create another document called Logout.php then copy and paste the following:

CODE

<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("Error".mysql_error()); }
mysql_select_db("Bennett",$link);
mysql_query("delete from chat where userid not in (130,131,132)");
?>



Output:

First web page will look like:

chat1.gif

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

chat2.gif

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

chat3.gif

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

chat4.gif

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

chat5.gif

After successfully logged out next page will be look like:

chat6.gif

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

Mon Feb 1, 2010    Reply    New Discussion   


Nice, and seems very simple. Let me try it!

Mon Feb 1, 2010    Reply    New Discussion   

QUOTE (yordan)

Nice, and seems very simple. Let me try it!
Link: view Post: 144582


It's very adaptable, obviously this is just the simplified version. You can add all the nice little designs and aesthetics afterwards. I wrote this tutorial over year ago and thought I would look up the original to add to these forums, however my old post was unfortunately deleted. I did find someone who had taken my original tutorial and tried to pass it off as his own (which as you can imagine REALLY annoyed me) but it was handy for using the images they had uploaded so I re-wrote it using their images and am waiting for them to get back to me regarding the theft of my tutorial.

Turns out there are a couple of my old tutorials being passed off as their own so I'm pretty angry right now!

Mon Feb 1, 2010    Reply    New Discussion   

Lot's of files. I'm sure there is a better way to do it, but great work anyways.

Tue Feb 2, 2010    Reply    New Discussion   


you know what....i think i am going to use this scripts,remake them and use them on my site when i'm done with it,it will make great addition to it...
good job,and keep on helping people with your own ideas and sharing them...

thanks...Eggie

Tue Feb 2, 2010    Reply    New Discussion   

QUOTE (HannahI)

Lot's of files. I'm sure there is a better way to do it, but great work anyways.
Link: view Post: 144627


Like I said this is very basic. The next one i'm releasing is a shoutbox that uses only two php files (one being the page you want the shoutbox on) and an ajax javascript file and it will teach more in depth design and functionaility.

Tue Feb 2, 2010    Reply    New Discussion   

QUOTE (8ennett)

Like I said this is very basic. The next one i'm releasing is a shoutbox that uses only two php files (one being the page you want the shoutbox on) and an ajax javascript file and it will teach more in depth design and functionaility.
Link: view Post: 144632

i am looking forward to it...i think you can do even better,and keep them coming

Thanks...Eggie

Wed Feb 3, 2010    Reply    New Discussion   

Quickly Post to Simple Chat System Create your own basic chat system using PHP! w/o signup Share Info about Simple Chat System Create your own basic chat system using PHP! using Facebook, Twitter etc. email your friend about Simple Chat System Create your own basic chat system using PHP! Print
Reply / Comment Ask a Question? Share / Bookmark E-Mail a Friend Print

Image Verification Script Add A Security Image To Your Web Forms Easily  Image Verification Script Add A Security Image To Your Web Forms Easily (2) (2) Very Simple Shoutbox Add a very simple shoutbox to your site with PHP and MySQL  Very Simple Shoutbox Add a very simple shoutbox to your site with PHP and MySQL