Everyone is familiar with chats. But when you perform chats in social networking sites did you ever think of creating by ourselfs. here iam publishing a code of a simple chat system using php..... It is a basic chat system which refreshes every minute and u can perform noramal chat...
For this first you have to create a databse with name chat please import the following code to make the database:-
CREATE TABLE IF NOT EXISTS `chat` (
`time` int(11) NOT NULL,
`name` varchar(30) NOT NULL,
`ip` varchar(15) NOT NULL,
`message` varchar(255) NOT NULL,
PRIMARY KEY (`time`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Now you have to create files for the simple chat system.
Below iam providing three files,you can copy paste from here:-
1)chat.php
........................
<html>
<head>
<style>
.message {
overflow:hidden;
width:498px;
margin-bottom:5px;
border:1px solid #999;
}
.messagehead {
overflow:hidden;
background:#FFC;
width:500px;
}
.messagecontent {
overflow:hidden;
width:496px;
}
</style>
</head>
<body style="background:cyan">
<h1> <span style="color:red; float:center">Chat system</span></h1>
<div id="chat" style="width:500px;margin:0 auto;overflow:hidden;">
<div id="messages"></div>
<div id="error" style="width:500px;text-align:center;color:red;"></div>
<div id="write" style="text-align:center;"><textarea id="message" cols="50" rows="5"></textarea><br/>Name:<input type="text" id="name"/><input type="button" value="Send" onClick="send();"/></div>
</div>
<script type="text/javascript">
function showmessages(){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","show-messages.php?" + Math.random(),false);
xmlhttp.send(null);
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","showmessages.php?" + Math.random(),false);
xmlhttp.send();
}
document.getElementById('messages').innerHTML = xmlhttp.responseText;
setTimeout('showmessages()',30000);
}
showmessages();
function send(){
var sendto = 'send.php?message=' + document.getElementById('message').value + '&name=' + document.getElementById('name').value;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET",sendto,false);
xmlhttp.send(null);
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET",sendto,false);
xmlhttp.send();
}
var error = '';
switch(parseInt(xmlhttp.responseText)){
case 1:
error = 'The database is down!';
break;
case 2:
error = 'The database is down!';
break;
case 3:
error = 'Don`t forget the message!';
break;
case 4:
error = 'The message is too long!';
break;
case 5:
error = 'Don`t forget the name!';
break;
case 6:
error = 'The name is too long!';
break;
case 7:
error = 'This name is already used by somebody else!';
break;
case 8:
error = 'The database is down!';
}
if(error == ''){
document.getElementById('error').innerHTML = '';
showmessages();
}
else{
document.getElementById('error').innerHTML = error;
}
}
</script>
</body>
</html>
2)send.php
...................................
<?php
mysql_connect('localhost', 'root', 'dbadmin') or die (1);
mysql_select_db('chat') or die (2);
$message = $_GET['message'];
$name = $_GET['name'];
if(strlen($message) < 1){
echo 3;
}
else if(strlen($message) > 255){
echo 4;
}
else if(strlen($name) < 1){
echo 5;
}
else if(strlen($name) > 29){
echo 6;
}
else if(mysql_num_rows(mysql_query("select * from chat where name = '" . $name . "' and ip != '" . @$REMOTE_ADDR . "'")) != 0){
echo 7;
}
else{
$search = array("<",">",">","<");
mysql_query("insert into chat values ('" . time() . "', '" . str_replace($search,"",$name) . "', '" . @$REMOTE_ADDR . "', '" . str_replace($search,"",$message) . "')") or die(8);
}
?>
3)show-messages.php
........................................
<?php
mysql_connect('localhost', 'root', 'dbadmin') or die (1);
mysql_select_db('chat') or die (2);
if(isset($_GET['message'])){
$message = $_GET['message'];
}
if(isset($_GET['name'])){
$name = $_GET['name'];
}
$result = mysql_query("select * from chat order by time desc limit 0,10");
$messages = array();
while($row = mysql_fetch_array($result)){
$messages[] = "<div class='message'><div class='messagehead'>" . $row['name'] . " - " . date('g:i A M, d Y',$row['time']) . "</div><div class='messagecontent'>" . $row['message'] . "</div></div>";
$old = $row['time'];
}
for($i=count($result);$i>=0;$i--){
echo $messages[$i];
}
mysql_query("delete from chat where time < " . $old);
?>
put all files in a folder now take localhost/foldername/chat.php and enjoy the chat
For this first you have to create a databse with name chat please import the following code to make the database:-
CREATE TABLE IF NOT EXISTS `chat` (
`time` int(11) NOT NULL,
`name` varchar(30) NOT NULL,
`ip` varchar(15) NOT NULL,
`message` varchar(255) NOT NULL,
PRIMARY KEY (`time`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Now you have to create files for the simple chat system.
Below iam providing three files,you can copy paste from here:-
1)chat.php
........................
<html>
<head>
<style>
.message {
overflow:hidden;
width:498px;
margin-bottom:5px;
border:1px solid #999;
}
.messagehead {
overflow:hidden;
background:#FFC;
width:500px;
}
.messagecontent {
overflow:hidden;
width:496px;
}
</style>
</head>
<body style="background:cyan">
<h1> <span style="color:red; float:center">Chat system</span></h1>
<div id="chat" style="width:500px;margin:0 auto;overflow:hidden;">
<div id="messages"></div>
<div id="error" style="width:500px;text-align:center;color:red;"></div>
<div id="write" style="text-align:center;"><textarea id="message" cols="50" rows="5"></textarea><br/>Name:<input type="text" id="name"/><input type="button" value="Send" onClick="send();"/></div>
</div>
<script type="text/javascript">
function showmessages(){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","show-messages.php?" + Math.random(),false);
xmlhttp.send(null);
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","showmessages.php?" + Math.random(),false);
xmlhttp.send();
}
document.getElementById('messages').innerHTML = xmlhttp.responseText;
setTimeout('showmessages()',30000);
}
showmessages();
function send(){
var sendto = 'send.php?message=' + document.getElementById('message').value + '&name=' + document.getElementById('name').value;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET",sendto,false);
xmlhttp.send(null);
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET",sendto,false);
xmlhttp.send();
}
var error = '';
switch(parseInt(xmlhttp.responseText)){
case 1:
error = 'The database is down!';
break;
case 2:
error = 'The database is down!';
break;
case 3:
error = 'Don`t forget the message!';
break;
case 4:
error = 'The message is too long!';
break;
case 5:
error = 'Don`t forget the name!';
break;
case 6:
error = 'The name is too long!';
break;
case 7:
error = 'This name is already used by somebody else!';
break;
case 8:
error = 'The database is down!';
}
if(error == ''){
document.getElementById('error').innerHTML = '';
showmessages();
}
else{
document.getElementById('error').innerHTML = error;
}
}
</script>
</body>
</html>
2)send.php
...................................
<?php
mysql_connect('localhost', 'root', 'dbadmin') or die (1);
mysql_select_db('chat') or die (2);
$message = $_GET['message'];
$name = $_GET['name'];
if(strlen($message) < 1){
echo 3;
}
else if(strlen($message) > 255){
echo 4;
}
else if(strlen($name) < 1){
echo 5;
}
else if(strlen($name) > 29){
echo 6;
}
else if(mysql_num_rows(mysql_query("select * from chat where name = '" . $name . "' and ip != '" . @$REMOTE_ADDR . "'")) != 0){
echo 7;
}
else{
$search = array("<",">",">","<");
mysql_query("insert into chat values ('" . time() . "', '" . str_replace($search,"",$name) . "', '" . @$REMOTE_ADDR . "', '" . str_replace($search,"",$message) . "')") or die(8);
}
?>
3)show-messages.php
........................................
<?php
mysql_connect('localhost', 'root', 'dbadmin') or die (1);
mysql_select_db('chat') or die (2);
if(isset($_GET['message'])){
$message = $_GET['message'];
}
if(isset($_GET['name'])){
$name = $_GET['name'];
}
$result = mysql_query("select * from chat order by time desc limit 0,10");
$messages = array();
while($row = mysql_fetch_array($result)){
$messages[] = "<div class='message'><div class='messagehead'>" . $row['name'] . " - " . date('g:i A M, d Y',$row['time']) . "</div><div class='messagecontent'>" . $row['message'] . "</div></div>";
$old = $row['time'];
}
for($i=count($result);$i>=0;$i--){
echo $messages[$i];
}
mysql_query("delete from chat where time < " . $old);
?>
put all files in a folder now take localhost/foldername/chat.php and enjoy the chat
great
ReplyDelete