Jump to content
xisto Community
8ennett

Simple Chat System Create your own basic chat system using PHP

Recommended Posts

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:

 

"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 _linenums:0'><?php session_start(); ?> <html> <head> <script type="text/javascript"> <strong class='bbc'>function</strong> nick() { <strong class='bbc'>var</strong> nick=document.chat.user.value; res=<strong class='bbc'>true</strong>; <strong class='bbc'>if</strong>(nick=="") { alert("please enter a value"); //document.tchat.nick.focus(); res= <strong class='bbc'>false</strong>; } <strong class='bbc'>return</strong> 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:

 


$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> _linenums:0'><?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:

 

"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> _linenums:0'><script type="text/javascript"> <strong class='bbc'>function</strong> message(){ result=<strong class='bbc'>true</strong>; mesg=document.msg.msg.value; <strong class='bbc'>if</strong>(mesg==""){ alert("Please fill any message"); result= <strong class='bbc'>false</strong>; } <strong class='bbc'>return</strong> 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:

 

"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']." _linenums:0'><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:

 

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

?> _linenums:0'><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:

 

Posted Image

 

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

 

Posted Image

 

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

 

Posted Image

 

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

 

Posted Image

 

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

 

Posted Image

 

After successfully logged out next page will be look like:

 

Posted Image

 

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

Share this post


Link to post
Share on other sites

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

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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

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.

Share this post


Link to post
Share on other sites

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.

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.