Chat application with XMLHttp

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

User avatar
Burrito
Spockulator
Posts: 4714
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Chat application with XMLHttp

Post by Burrito »

////////////////////////
EDIT: I've had a few people try to chat with FF and it's failed. I have not thoroughly tested with FF but have had mixed experiences with it (mostly good). But it's apparent there are some that are bad which are causing it to not work at all. I know this isn't what you want to hear, but for demonstration purposes, might be better to try hitting it with IE :?

I'll let all of you geniouses work out the FF bugs
////////////////////////

I didn't want to post this just yet because it still has some bugs (especially with firefox) but after sooo many people requesting information about chat applications, I thought this could be a good resource for people looking to create their own with somethign other than java/flash and the like.

I do ask that if you modify it in any way, that you please provide me with the changes so that I can incorporate them into my version (assuming I like them :)).

it uses subqueries so 4.1 is required on the db end.

i have it set up here and if you click that link and join the chat, it will email me so I can give you a short demonstration.

I made this quite some time ago and havent' had the time/desire (more desire than time) to fix the small issues that have come up. It's a good start though and I'll bet if you put a little work and effort into it, you could make it super spicy...but remmeber to keep me posted on the spice.

Code: Select all

-- phpMyAdmin SQL Dump
-- version 2.6.1-rc2
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Mar 13, 2005 at 07:42 PM
-- Server version: 4.1.10
-- PHP Version: 5.0.3
--
-- Database: `chat`
--

-- --------------------------------------------------------

--
-- Table structure for table `chatcli`
--

DROP TABLE IF EXISTS `chatcli`;
CREATE TABLE IF NOT EXISTS `chatcli` (
  `id` mediumint(9) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `cliid` mediumint(9) default NULL,
  `time` datetime default NULL,
  `chattext` longtext,
  `room` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  KEY `time` (`time`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=382 ;

-- --------------------------------------------------------

--
-- Table structure for table `chatread`
--

DROP TABLE IF EXISTS `chatread`;
CREATE TABLE IF NOT EXISTS `chatread` (
  `id` mediumint(9) NOT NULL auto_increment,
  `chatid` mediumint(9) default NULL,
  `cliid` mediumint(9) default NULL,
  `name` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  KEY `cliid` (`cliid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=800 ;

-- --------------------------------------------------------

--
-- Table structure for table `chatusers`
--

DROP TABLE IF EXISTS `chatusers`;
CREATE TABLE IF NOT EXISTS `chatusers` (
  `id` mediumint(9) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `time` datetime default NULL,
  `lasttime` datetime default NULL,
  `room` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  KEY `room` (`room`),
  KEY `lasttime` (`lasttime`),
  KEY `time` (`time`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=53 ;

Code: Select all

<?
////////////////////////////////////////////////////////////
//                  BreakFast Chat v1.0						
//						  Authors:							
//						  Burrito(burrito@burritostand.com) 
//						  SMPDawg							
//					Last Modified: 04/28/2005				
//					 Created: 3/12/2005						
//  Please feel free to use/distribute/modify this code		
//  I only ask that if you make any improvements that you	
//  email me the updated code so I can incorporate it into  
//  my version  burrito@burritostand.com.  I also ask 	
//  that you retain this copy © info at the top of every	
//  page of the application.								
//  enjoy!!													
//copyright © 2005 Burrito (burrito AT burritostand DOT Com)
////////////////////////////////////////////////////////////


require_once(&quote;../includes/dbInteract.php&quote;);
tt_db_connect();
if(isset($_GETї'invite'])){
	mysql_query(&quote;insert into 21_chatreq
	(
	req,
	userid,
	reqname,
	reqtime
	)
	values
	(
	&quote;.$_GETї'rm'].&quote;,
	&quote;.$_GETї'invite'].&quote;,
	'&quote;.mysql_escape_string($_GETї'name']).&quote;',
	now()
	)
	&quote;)
		or die(mysql_error());
}
$rm = (isset($_GETї'rm']) ? $_GETї'rm'] : &quote;General Chat&quote;);
$cliid = $_SERVERї&quote;REMOTE_ADDR&quote;];
$cliid = substr(md5($cliid), rand(0, 23), ;
$chatWho = (isset($_GETї'name']) ? $_GETї'name'] : &quote;Guest&quote;.$cliid);
$inssql = &quote;insert into 21_chatusers
(
name,
time,
lasttime,
room
)
values
(
'&quote;.mysql_escape_string($chatWho).&quote;',
now(),
now(),
'&quote;.mysql_escape_string($rm).&quote;'
)&quote;;
mysql_query($inssql)
  or die(mysql_error());
$cliid = mysql_insert_id();

?>

<!DOCTYPE HTML PUBLIC &quote;-//W3C//DTD HTML 4.01 Transitional//EN&quote;>

<html>
<head>
  <title>Chat</title>
<style>
input
{
background-color:#6F747D; border:1px #000000 solid; color:#ffffff; font-family:tahoma; font-size:8.5pt; padding-left:2px;
}
textarea.sid
{
width:400px; height:100px; background-color:#EBEADB; border:1px #000000 solid; color:#000000; font-family:tahoma; font-size:9.0pt; padding-left:2px;
}
.dsty
{
width:400px; height:400px; background-color:#EDEDE2; border:1px #000000 solid; color:#353519; overflow:auto;  font-family:tahoma; font-size:9.0pt; padding-left:2px; padding-top:2px;
}
td.le
{
border-left:1px #2E3E3F solid; border-top:1px #2E3E3F solid; border-bottom:1px #2E3E3F solid;
}
td.rt
{
border-right:1px #2E3E3F solid; border-top:1px #2E3E3F solid; border-bottom:1px #2E3E3F solid; padding-top:2px;
}
td.rb
{
border-left:1px #2E3E3F solid;
}
.parts
{
width:190px; height:270px; background-color:#68768E; border:1px #000000 solid; color:#ffffff; overflow:auto;  font-family:tahoma; font-size:8.5pt; padding-left:2px; padding-top:2px; text-align:center;
}
</style>
<script>
function addIt(what,who,color,to){
  var clr = color;
  var temp = document.createElement(&quote;div&quote;);
  temp.style.color = clr;
  if(!document.all){
    temp.innerHTML = who + &quote;: &quote; + what;
  }else{
    temp.innerText = who + &quote;: &quote; + what;
  }
  if (temp.innerHTML); else
    temp.appendChild(document.createTextNode(what));
  if (temp.innerHTML){
    var html = temp.innerHTML;
    html = html.replace(/(\r?\n)/g, &quote;<br>$1&quote;);
    html = html.replace(/(їa-zA-Z_0-9.-]+\@їa-zA-Z_0-9.-]+\.\w+)/g, &quote;<a href=\&quote;mailto:$1\&quote;>$1</a>&quote;);
    html = html.replace(new RegExp(&quote;(\\w+ї\\://]+\\w+\\.\\w+\\.\\w+ї/\\w+]*ї.\\w+]*)&quote;, &quote;g&quote;), &quote;<a href=\&quote;$1\&quote; target=\&quote;_blank\&quote;>$1</a>&quote;);
    html = html.replace(new RegExp(&quote;(\\\\')&quote;, &quote;g&quote;),&quote;'&quote;);
    temp.innerHTML = html;
  }
  if(to){
    sendIt();
    setTimeout(&quote;clearIt()&quote;,0);
  }
  document.getElementById(&quote;rvie&quote;).appendChild(temp);
    temp.scrollIntoView();
  document.MyForm.rtex.focus();
}

function clearIt(){
	document.MyForm.rtex.value = &quote;&quote;;
}







function checkEnt(evt){
  if(evt.keyCode == 13 && !evt.shiftKey){
      addIt(document.MyForm.rtex.value,'TD','#222755','from');
    
    evt.returnValue = false;
	

    if (evt.preventDefault)
    evt.preventDefault();

  }
}

if (window.XMLHttpRequest){
  req = new XMLHttpRequest();
}else{
  req = new ActiveXObject(&quote;Microsoft.XMLHTTP&quote;);
}

if (window.XMLHttpRequest){
  reqs = new XMLHttpRequest();
}else{
  reqs = new ActiveXObject(&quote;Microsoft.XMLHTTP&quote;);
}
// SMPDawg - Added another object to send messages and (hopefully) prevent the chat
// application from trying to send/receive from one XMLHTTP object.
if (window.XMLHttpRequest){
  reqsend = new XMLHttpRequest();
}else{
  reqsend = new ActiveXObject(&quote;Microsoft.XMLHTTP&quote;);
}
whoid = 0;
function retrieveIt(simid) {
  samid = (!simid ? 0 : simid);
  datevar = new Date();
  y = datevar.getFullYear();
  m = datevar.getMonth() + 1;
  d = datevar.getDate();
  h = datevar.getHours();
  i = datevar.getMinutes();
  s = datevar.getSeconds();
  formatted = y+&quote;-&quote;+m+&quote;-&quote;+d+&quote; &quote;+h+&quote;:&quote;+i+&quote;:&quote;+s;
  req.open(&quote;GET&quote;, &quote;getchat.php?Timestr=&quote;+escape(formatted)+&quote;&nm=1&cliid=<?=$cliid;?>&room=<?=$rm;?>&rid=&quote;+samid, true);
  req.onreadystatechange = function(){
    if (req.readyState == 4 && req.status == 200) {

      if(req.responseText.indexOf(&quote;**&quote;) != -1){

        whot = req.responseText.split(&quote;**&quote;);
        whoid = whotї0];
        whoto = whotї1];
        whota = whotї2];
        addIt(whota,whoto,&quote;#353519&quote;);

      }else{
        whoid = 0;
      }
    }
  }
    req.send(null);

  setTimeout(&quote;retrieveIt(whoid)&quote;,1000);
}

// SMPDawg - Changed reqs to reqsend per the previous change.
function sendIt() {
  datevar = new Date();
  y = datevar.getFullYear();
  m = datevar.getMonth() + 1;
  d = datevar.getDate();
  h = datevar.getHours();
  i = datevar.getMinutes();
  s = datevar.getSeconds();
  formatted = y+&quote;-&quote;+m+&quote;-&quote;+d+&quote; &quote;+h+&quote;:&quote;+i+&quote;:&quote;+s;
  reqsend.onreadystatechange = function(){
    if (reqsend.readyState == 4 && reqsend.status == 200) {

    }
  };
  reqsend.open(&quote;POST&quote;, &quote;getchat.php&quote;, true);
  var stuff = &quote;rtex=&quote; + escape(document.MyForm.rtex.value) + &quote;&name=&quote; + escape(document.MyForm.name.value) + &quote;&cliid=&quote; + document.MyForm.cliid.value + &quote;&room=&quote; + escape(document.MyForm.room.value);
  reqsend.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  reqsend.setRequestHeader(&quote;Content-Length&quote;,stuff.length);
  reqsend.send(stuff);


}
clist = 0;
function whosIn() {
  datevar = new Date();
  y = datevar.getFullYear();
  m = datevar.getMonth() + 1;
  d = datevar.getDate();
  h = datevar.getHours();
  i = datevar.getMinutes();
  s = datevar.getSeconds();
  formatted = y+&quote;-&quote;+m+&quote;-&quote;+d+&quote; &quote;+h+&quote;:&quote;+i+&quote;:&quote;+s;
  reqs.open(&quote;GET&quote;, &quote;getparts.php?Timestr=&quote;+escape(formatted)+&quote;&cliid=<?=$cliid;?>&room=<?=$rm;?>&clist=&quote; + clist, true);
  reqs.onreadystatechange = function(){
    if (reqs.readyState == 4 && reqs.status == 200) {

      if(reqs.responseText.indexOf(&quote;**&quote;) != -1){
        whot = reqs.responseText.split(&quote;**&quote;);
        document.getElementById('pts').innerHTML = &quote;<u>Chat Room</u><br>&quote; + whotї1];
        clist = whotї0];
      }
    }
  }
  reqs.send(null);

  setTimeout(&quote;whosIn()&quote;,1000);
}
function addPart(idd,namer){

  document.getElementById('pts').innerHTML += namer + &quote;<br>&quote;;
}
</script>
</head>

<body onLoad=&quote;retrieveIt(), whosIn(), document.MyForm.rtex.focus()&quote; topmargin=&quote;0&quote; marginheight=&quote;0&quote; leftmargin=&quote;0&quote; marginwidth=&quote;0&quote;>
<form name=&quote;MyForm&quote; method=&quote;post&quote;>
<table width=&quote;600&quote; cellspacing=&quote;0&quote; cellpadding=&quote;0&quote;>
<tr>
<td rowspan=&quote;2&quote; width=&quote;400&quote; class=&quote;le&quote;>
  <table width=&quote;100%&quote; cellpadding=&quote;2&quote; cellspacing=&quote;0&quote;>
  <tr>
  <td>
  <div id="rvie" class="dsty"></div>

&lt;textarea name=&quote;rtex&quote; class=&quote;sid&quote; onKeyDown=&quote;checkEnt(event)&quote; &gt;&lt;/textarea&gt;&lt;br&gt;
&lt;input type=&quote;button&quote; value=&quote;Send Message&quote; onClick=&quote;addIt(document.MyForm.rtex.value,'&lt;?=$chatWho;?&gt;','#222755','from')&quote; style=&quote;width:400px; align:center&quote;&gt;

&lt;input type=&quote;hidden&quote; name=&quote;name&quote; value=&quote;&lt;?=$chatWho;?&gt;&quote;&gt;
&lt;input type=&quote;hidden&quote; name=&quote;cliid" value="<?=$cliid;?>">
<input type="hidden" name=&quote;room&quote; value=&quote;&lt;?=$rm;?&gt;&quote;&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;


&lt;/td&gt;
&lt;td class=&quote;rt&quote; align=&quote;center&quote; valign=&quote;top&quote; height=&quote;270&quote;&gt;
&lt;div id=&quote;pts&quote; class=&quote;parts&quote;&gt;


&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quote;rb&quote; height=&quote;230&quote;&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr>
</table>
</form>
</body>
</html>

Code: Select all

&lt;?
////////////////////////////////////////////////////////////
//                  BreakFast Chat v1.0						
//						  Authors:							
//						  Burrito(burrito@burritostand.com) 
//						  SMPDawg							
//					Last Modified: 04/28/2005				
//					 Created: 3/12/2005						
//  Please feel free to use/distribute/modify this code		
//  I only ask that if you make any improvements that you	
//  email me the updated code so I can incorporate it into  
//  my version  burrito@burritostand.com.  I also ask 	
//  that you retain this copy © info at the top of every	
//  page of the application.								
//  enjoy!!													
//copyright © 2005 Burrito (burrito AT burritostand DOT Com)
////////////////////////////////////////////////////////////

// refresh this page every time it's hit...burrito
header(&quote;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quote;); 
header(&quote;Last-Modified: &quote; . gmdate(&quote;D, d M Y H:i:s&quote;) . &quote; GMT&quote;); 
header(&quote;Cache-Control: no-store, no-cache, must-revalidate&quote;); 
header(&quote;Cache-Control: post-check=0, pre-check=0&quote;, false); 
header(&quote;Pragma: no-cache&quote;); 
// grab db connect info....burrito
require_once(&quote;../includes/dbInteract.php&quote;);
tt_db_connect();

// check to see if this is a request for chat messages...burrito
if(isset($_GET&#1111;'nm'])){
	// update user table to show they are still logged in...burrito
	mysql_query(&quote;update 21_chatusers set lasttime = now() where id = &quote;.$_GET&#1111;'cliid'])
		or die(mysql_error());
	// check to see if need to insert a read message row...burrito
	if(isset($_GET&#1111;'rid']) &amp;&amp; $_GET&#1111;'rid'] != 0){
		$sql = &quote;insert into 21_chatread (chatid,cliid) values (&quote;.$_GET&#1111;'rid'].&quote;,&quote;.$_GET&#1111;'cliid'].&quote;)&quote;;
		mysql_query($sql)
			or die(mysql_error());
	} // end if for need to insert a read message...burrito
	$graqu = &quote;select * from 21_chatcli WHERE id NOT IN (SELECT chatid FROM 21_chatread where cliid = &quote;.$_GET&#1111;'cliid'].&quote;) and cliid &lt;&gt; &quote;.$_GET&#1111;'cliid'].&quote; and time > (select time from 21_chatusers where id = ".$_GET['cliid'].") and room = '".mysql_escape_string($_GET&#1111;'room']).&quote;'&quote;;
	$getmsgs = mysql_query($graqu)
		or die(mysql_error());;
	if($gtmsgs = mysql_fetch_assoc($getmsgs)){
		// send back the chat messages that have not been read...burrito
		echo $gtmsgs&#1111;'id'].&quote;**&quote;.$gtmsgs&#1111;'name'].&quote;**&quote;.$gtmsgs&#1111;'chattext'];
	}
}else{ // not a message request, so insert new message...burrito
	mysql_query(&quote;insert into 21_chatcli 
	(
	name,
	cliid,
	time,
	chattext,
	room
	) 
	values 
	(
	'&quote;.$_POST&#1111;'name'].&quote;',
	&quote;.$_POST&#1111;'cliid'].&quote;,
	now(),
	'&quote;.mysql_escape_string($_POST&#1111;'rtex']).&quote;',
	'&quote;.mysql_escape_string($_POST&#1111;'room']).&quote;'
	)")
		or die("**".mysql_error());;
} // end if for message request or new message...burrito
?>

Code: Select all

&lt;?
////////////////////////////////////////////////////////////
//                  BreakFast Chat v1.0						
//						  Authors:							
//						  Burrito(burrito@burritostand.com) 
//						  SMPDawg							
//					Last Modified: 04/28/2005				
//					 Created: 3/12/2005						
//  Please feel free to use/distribute/modify this code		
//  I only ask that if you make any improvements that you	
//  email me the updated code so I can incorporate it into  
//  my version  burrito@burritostand.com.  I also ask 	
//  that you retain this copy © info at the top of every	
//  page of the application.								
//  enjoy!!													
//copyright © 2005 Burrito (burrito AT burritostand DOT Com)
////////////////////////////////////////////////////////////

// refresh this page every time it's hit...burrito
header(&quote;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quote;); 
header(&quote;Last-Modified: &quote; . gmdate(&quote;D, d M Y H:i:s&quote;) . &quote; GMT&quote;); 
header(&quote;Cache-Control: no-store, no-cache, must-revalidate&quote;); 
header(&quote;Cache-Control: post-check=0, pre-check=0&quote;, false); 
header(&quote;Pragma: no-cache&quote;); 
// grab db connect info....burrito
require_once(&quote;../includes/dbInteract.php&quote;);
tt_db_connect();
// grab participants in current room...burrito
$partsqu = &quote;select * from 21_chatusers where room = '&quote;.mysql_escape_string($_GET&#1111;'room']).&quote;' and lasttime &gt; '&quote;.date(&quote;Y-m-d H:i:s&quote;, strtotime(&quote;-10 seconds&quote;)).&quote;'&quote;;
$getparts = mysql_query($partsqu)
	or die(mysql_error());
	$thisamt = mysql_num_rows($getparts);
// check to see if this is initial load to return all participants...burrito
if($thisamt !== $_GET&#1111;'clist']){
	$stky = $thisamt.&quote;**&quote;;
	while($gtparts = mysql_fetch_assoc($getparts)){ // return one row at a time for new users joining...burrito
		$stky .= $gtparts&#1111;'name'].&quote;&lt;br&gt;&quote;;
	}// end while for result set...burrito
	echo $stky;
} // end if for need to send down new participant info...burrito
?&gt;
i have it set up here and if you click that link and join the chat, it will email me so I can give you a short demonstration.

have fun with it!ople try to chat with FF and it's failed. I have not thoroughly tested with FF but have had mixed experiences with it (mostly good). But it's apparent there are some that are bad which are causing it to not work at all. I know this isn't what you want to hear, but for demonstration purposes, might be better to try hitting it with IE :?

I'll let all of you geniouses work out the FF bugs
////////////////////////

I didn't want to post this just yet because it still has some bugs (especially with firefox) but after sooo many people requesting information about chat applications, I thought this could be a good resource for people looking to create their own with somethign other than java/flash and the like.

I do ask that if you modify it in any way, that you please provide me with the changes so that I can incorporate them into my version (assuming I like them :)).

it uses subqueries so 4.1 is required on the db end.

i have it set up here and if you click that link and join the chat, it will email me so I can give you a short demonstration.

I made this quite some time ago and havent' had the time/desire (more desire than time) to fix the small issues that have come up. It's a good start though and I'll bet if you put a little work and effort into it, you could make it super spicy...but remmeber to keep me posted on the spice.

Code: Select all

-- phpMyAdmin SQL Dump
-- version 2.6.1-rc2
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Mar 13, 2005 at 07:42 PM
-- Server version: 4.1.10
-- PHP Version: 5.0.3
--
-- Database: `chat`
--

-- --------------------------------------------------------

--
-- Table structure for table `chatcli`
--

DROP TABLE IF EXISTS `chatcli`;
CREATE TABLE IF NOT EXISTS `chatcli` (
  `id` mediumint(9) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `cliid` mediumint(9) default NULL,
  `time` datetime default NULL,
  `chattext` longtext,
  `room` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  KEY `time` (`time`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=382 ;

-- --------------------------------------------------------

--
-- Table structure for table `chatread`
--

DROP TABLE IF EXISTS `chatread`;
CREATE TABLE IF NOT EXISTS `chatread` (
  `id` mediumint(9) NOT NULL auto_increment,
  `chatid` mediumint(9) default NULL,
  `cliid` mediumint(9) default NULL,
  `name` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  KEY `cliid` (`cliid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=800 ;

-- --------------------------------------------------------

--
-- Table structure for table `chatusers`
--

DROP TABLE IF EXISTS `chatusers`;
CREATE TABLE IF NOT EXISTS `chatusers` (
  `id` mediumint(9) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `time` datetime default NULL,
  `lasttime` datetime default NULL,
  `room` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  KEY `room` (`room`),
  KEY `lasttime` (`lasttime`),
  KEY `time` (`time`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=53 ;

Code: Select all

&lt;?
////////////////////////////////////////////////////////////
//                  BreakFast Chat v1.0						
//						  Authors:							
//						  Burrito(burrito@burritostand.com) 
//						  SMPDawg							
//					Last Modified: 04/28/2005				
//					 Created: 3/12/2005						
//  Please feel free to use/distribute/modify this code		
//  I only ask that if you make any improvements that you	
//  email me the updated code so I can incorporate it into  
//  my version  burrito@burritostand.com.  I also ask 	
//  that you retain this copy © info at the top of every	
//  page of the application.								
//  enjoy!!													
//copyright © 2005 Burrito (burrito AT burritostand DOT Com)
////////////////////////////////////////////////////////////


require_once(&quote;../includes/dbInteract.php&quote;);
tt_db_connect();
if(isset($_GET&#1111;'invite'])){
	mysql_query(&quote;insert into 21_chatreq
	(
	req,
	userid,
	reqname,
	reqtime
	)
	values
	(
	&quote;.$_GET&#1111;'rm'].&quote;,
	&quote;.$_GET&#1111;'invite'].&quote;,
	'&quote;.mysql_escape_string($_GET&#1111;'name']).&quote;',
	now()
	)
	&quote;)
		or die(mysql_error());
}
$rm = (isset($_GET&#1111;'rm']) ? $_GET&#1111;'rm'] : &quote;General Chat&quote;);
$cliid = $_SERVER&#1111;&quote;REMOTE_ADDR&quote;];
$cliid = substr(md5($cliid), rand(0, 23), ;
$chatWho = (isset($_GET&#1111;'name']) ? $_GET&#1111;'name'] : &quote;Guest&quote;.$cliid);
$inssql = &quote;insert into 21_chatusers
(
name,
time,
lasttime,
room
)
values
(
'&quote;.mysql_escape_string($chatWho).&quote;',
now(),
now(),
'&quote;.mysql_escape_string($rm).&quote;'
)&quote;;
mysql_query($inssql)
  or die(mysql_error());
$cliid = mysql_insert_id();

?&gt;

&lt;!DOCTYPE HTML PUBLIC &quote;-//W3C//DTD HTML 4.01 Transitional//EN&quote;&gt;

&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Chat&lt;/title&gt;
&lt;style&gt;
input
{
background-color:#6F747D; border:1px #000000 solid; color:#ffffff; font-family:tahoma; font-size:8.5pt; padding-left:2px;
}
textarea.sid
{
width:400px; height:100px; background-color:#EBEADB; border:1px #000000 solid; color:#000000; font-family:tahoma; font-size:9.0pt; padding-left:2px;
}
.dsty
{
width:400px; height:400px; background-color:#EDEDE2; border:1px #000000 solid; color:#353519; overflow:auto;  font-family:tahoma; font-size:9.0pt; padding-left:2px; padding-top:2px;
}
td.le
{
border-left:1px #2E3E3F solid; border-top:1px #2E3E3F solid; border-bottom:1px #2E3E3F solid;
}
td.rt
{
border-right:1px #2E3E3F solid; border-top:1px #2E3E3F solid; border-bottom:1px #2E3E3F solid; padding-top:2px;
}
td.rb
{
border-left:1px #2E3E3F solid;
}
.parts
{
width:190px; height:270px; background-color:#68768E; border:1px #000000 solid; color:#ffffff; overflow:auto;  font-family:tahoma; font-size:8.5pt; padding-left:2px; padding-top:2px; text-align:center;
}
&lt;/style&gt;
&lt;script&gt;
function addIt(what,who,color,to){
  var clr = color;
  var temp = document.createElement(&quote;div&quote;);
  temp.style.color = clr;
  if(!document.all){
    temp.innerHTML = who + &quote;: &quote; + what;
  }else{
    temp.innerText = who + &quote;: &quote; + what;
  }
  if (temp.innerHTML); else
    temp.appendChild(document.createTextNode(what));
  if (temp.innerHTML){
    var html = temp.innerHTML;
    html = html.replace(/(\r?\n)/g, &quote;&lt;br&gt;$1&quote;);
    html = html.replace(/(&#1111;a-zA-Z_0-9.-]+\@&#1111;a-zA-Z_0-9.-]+\.\w+)/g, &quote;&lt;a href=\&quote;mailto:$1\&quote;&gt;$1&lt;/a&gt;&quote;);
    html = html.replace(new RegExp(&quote;(\\w+&#1111;\\://]+\\w+\\.\\w+\\.\\w+&#1111;/\\w+]*&#1111;.\\w+]*)&quote;, &quote;g&quote;), &quote;&lt;a href=\&quote;$1\&quote; target=\&quote;_blank\&quote;&gt;$1&lt;/a&gt;&quote;);
    html = html.replace(new RegExp(&quote;(\\\\')&quote;, &quote;g&quote;),&quote;'&quote;);
    temp.innerHTML = html;
  }
  if(to){
    sendIt();
    setTimeout(&quote;clearIt()&quote;,0);
  }
  document.getElementById(&quote;rvie&quote;).appendChild(temp);
    temp.scrollIntoView();
  document.MyForm.rtex.focus();
}

function clearIt(){
	document.MyForm.rtex.value = &quote;&quote;;
}







function checkEnt(evt){
  if(evt.keyCode == 13 &amp;&amp; !evt.shiftKey){
      addIt(document.MyForm.rtex.value,'TD','#222755','from');
    
    evt.returnValue = false;
	

    if (evt.preventDefault)
    evt.preventDefault();

  }
}

if (window.XMLHttpRequest){
  req = new XMLHttpRequest();
}else{
  req = new ActiveXObject(&quote;Microsoft.XMLHTTP&quote;);
}

if (window.XMLHttpRequest){
  reqs = new XMLHttpRequest();
}else{
  reqs = new ActiveXObject(&quote;Microsoft.XMLHTTP&quote;);
}
// SMPDawg - Added another object to send messages and (hopefully) prevent the chat
// application from trying to send/receive from one XMLHTTP object.
if (window.XMLHttpRequest){
  reqsend = new XMLHttpRequest();
}else{
  reqsend = new ActiveXObject(&quote;Microsoft.XMLHTTP&quote;);
}
whoid = 0;
function retrieveIt(simid) {
  samid = (!simid ? 0 : simid);
  datevar = new Date();
  y = datevar.getFullYear();
  m = datevar.getMonth() + 1;
  d = datevar.getDate();
  h = datevar.getHours();
  i = datevar.getMinutes();
  s = datevar.getSeconds();
  formatted = y+&quote;-&quote;+m+&quote;-&quote;+d+&quote; &quote;+h+&quote;:&quote;+i+&quote;:&quote;+s;
  req.open(&quote;GET&quote;, &quote;getchat.php?Timestr=&quote;+escape(formatted)+&quote;&amp;nm=1&amp;cliid=&lt;?=$cliid;?&gt;&amp;room=&lt;?=$rm;?&gt;&amp;rid=&quote;+samid, true);
  req.onreadystatechange = function(){
    if (req.readyState == 4 &amp;&amp; req.status == 200) {

      if(req.responseText.indexOf(&quote;**&quote;) != -1){

        whot = req.responseText.split(&quote;**&quote;);
        whoid = whot&#1111;0];
        whoto = whot&#1111;1];
        whota = whot&#1111;2];
        addIt(whota,whoto,&quote;#353519&quote;);

      }else{
        whoid = 0;
      }
    }
  }
    req.send(null);

  setTimeout(&quote;retrieveIt(whoid)&quote;,1000);
}

// SMPDawg - Changed reqs to reqsend per the previous change.
function sendIt() {
  datevar = new Date();
  y = datevar.getFullYear();
  m = datevar.getMonth() + 1;
  d = datevar.getDate();
  h = datevar.getHours();
  i = datevar.getMinutes();
  s = datevar.getSeconds();
  formatted = y+&quote;-&quote;+m+&quote;-&quote;+d+&quote; &quote;+h+&quote;:&quote;+i+&quote;:&quote;+s;
  reqsend.onreadystatechange = function(){
    if (reqsend.readyState == 4 &amp;&amp; reqsend.status == 200) {

    }
  };
  reqsend.open(&quote;POST&quote;, &quote;getchat.php&quote;, true);
  var stuff = &quote;rtex=&quote; + escape(document.MyForm.rtex.value) + &quote;&amp;name=&quote; + escape(document.MyForm.name.value) + &quote;&amp;cliid=&quote; + document.MyForm.cliid.value + &quote;&amp;room=&quote; + escape(document.MyForm.room.value);
  reqsend.setRequestHeader(&quote;Content-Type&quote;,&quote;application/x-www-form-urlencoded&quote;);
  reqsend.setRequestHeader(&quote;Content-Length&quote;,stuff.length);
  reqsend.send(stuff);


}
clist = 0;
function whosIn() {
  datevar = new Date();
  y = datevar.getFullYear();
  m = datevar.getMonth() + 1;
  d = datevar.getDate();
  h = datevar.getHours();
  i = datevar.getMinutes();
  s = datevar.getSeconds();
  formatted = y+&quote;-&quote;+m+&quote;-&quote;+d+&quote; &quote;+h+&quote;:&quote;+i+&quote;:&quote;+s;
  reqs.open(&quote;GET&quote;, &quote;getparts.php?Timestr=&quote;+escape(formatted)+&quote;&amp;cliid=&lt;?=$cliid;?&gt;&amp;room=&lt;?=$rm;?&gt;&amp;clist=&quote; + clist, true);
  reqs.onreadystatechange = function(){
    if (reqs.readyState == 4 &amp;&amp; reqs.status == 200) {

      if(reqs.responseText.indexOf(&quote;**&quote;) != -1){
        whot = reqs.responseText.split(&quote;**&quote;);
        document.getElementById('pts').innerHTML = &quote;&lt;u&gt;Chat Room&lt;/u&gt;&lt;br&gt;&quote; + whot&#1111;1];
        clist = whot&#1111;0];
      }
    }
  }
  reqs.send(null);

  setTimeout(&quote;whosIn()&quote;,1000);
}
function addPart(idd,namer){

  document.getElementById('pts').innerHTML += namer + &quote;&lt;br&gt;&quote;;
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body onLoad=&quote;retrieveIt(), whosIn(), document.MyForm.rtex.focus()&quote; topmargin=&quote;0&quote; marginheight=&quote;0&quote; leftmargin=&quote;0&quote; marginwidth=&quote;0&quote;&gt;
&lt;form name=&quote;MyForm&quote; method=&quote;post&quote;&gt;
&lt;table width=&quote;600&quote; cellspacing=&quote;0&quote; cellpadding=&quote;0&quote;&gt;
&lt;tr&gt;
&lt;td rowspan=&quote;2&quote; width=&quote;400&quote; class=&quote;le&quote;&gt;
  &lt;table width=&quote;100%&quote; cellpadding=&quote;2&quote; cellspacing=&quote;0&quote;&gt;
  &lt;tr&gt;
  &lt;td&gt;
  &lt;div id=&quote;rvie&quote; class=&quote;dsty&quote;&gt;&lt;/div&gt;

&lt;textarea name=&quote;rtex&quote; class=&quote;sid&quote; onKeyDown=&quote;checkEnt(event)&quote; &gt;&lt;/textarea&gt;&lt;br&gt;
&lt;input type=&quote;button&quote; value=&quote;Send Message&quote; onClick=&quote;addIt(document.MyForm.rtex.value,'&lt;?=$chatWho;?&gt;','#222755','from')&quote; style=&quote;width:400px; align:center&quote;&gt;

&lt;input type=&quote;hidden&quote; name=&quote;name&quote; value=&quote;&lt;?=$chatWho;?&gt;&quote;&gt;
&lt;input type=&quote;hidden&quote; name=&quote;cliid&quote; value=&quote;&lt;?=$cliid;?&gt;&quote;&gt;
&lt;input type=&quote;hidden&quote; name=&quote;room&quote; value=&quote;&lt;?=$rm;?&gt;&quote;&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;


&lt;/td&gt;
&lt;td class=&quote;rt&quote; align=&quote;center&quote; valign=&quote;top&quote; height=&quote;270&quote;&gt;
&lt;div id=&quote;pts&quote; class=&quote;parts&quote;&gt;


&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quote;rb&quote; height=&quote;230&quote;&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Code: Select all

&lt;?
////////////////////////////////////////////////////////////
//                  BreakFast Chat v1.0						
//						  Authors:							
//						  Burrito(burrito@burritostand.com) 
//						  SMPDawg							
//					Last Modified: 04/28/2005				
//					 Created: 3/12/2005						
//  Please feel free to use/distribute/modify this code		
//  I only ask that if you make any improvements that you	
//  email me the updated code so I can incorporate it into  
//  my version  burrito@burritostand.com.  I also ask 	
//  that you retain this copy © info at the top of every	
//  page of the application.								
//  enjoy!!													
//copyright © 2005 Burrito (burrito AT burritostand DOT Com)
////////////////////////////////////////////////////////////

// refresh this page every time it's hit...burrito
header(&quote;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quote;); 
header(&quote;Last-Modified: &quote; . gmdate(&quote;D, d M Y H:i:s&quote;) . &quote; GMT&quote;); 
header(&quote;Cache-Control: no-store, no-cache, must-revalidate&quote;); 
header(&quote;Cache-Control: post-check=0, pre-check=0&quote;, false); 
header(&quote;Pragma: no-cache&quote;); 
// grab db connect info....burrito
require_once(&quote;../includes/dbInteract.php&quote;);
tt_db_connect();

// check to see if this is a request for chat messages...burrito
if(isset($_GET&#1111;'nm'])){
	// update user table to show they are still logged in...burrito
	mysql_query(&quote;update 21_chatusers set lasttime = now() where id = &quote;.$_GET&#1111;'cliid'])
		or die(mysql_error());
	// check to see if need to insert a read message row...burrito
	if(isset($_GET&#1111;'rid']) &amp;&amp; $_GET&#1111;'rid'] != 0){
		$sql = &quote;insert into 21_chatread (chatid,cliid) values (&quote;.$_GET&#1111;'rid'].&quote;,&quote;.$_GET&#1111;'cliid'].&quote;)&quote;;
		mysql_query($sql)
			or die(mysql_error());
	} // end if for need to insert a read message...burrito
	$graqu = &quote;select * from 21_chatcli WHERE id NOT IN (SELECT chatid FROM 21_chatread where cliid = &quote;.$_GET&#1111;'cliid'].&quote;) and cliid &lt;&gt; &quote;.$_GET&#1111;'cliid'].&quote; and time &gt; (select time from 21_chatusers where id = &quote;.$_GET&#1111;'cliid'].&quote;) and room = '&quote;.mysql_escape_string($_GET&#1111;'room']).&quote;'&quote;;
	$getmsgs = mysql_query($graqu)
		or die(mysql_error());;
	if($gtmsgs = mysql_fetch_assoc($getmsgs)){
		// send back the chat messages that have not been read...burrito
		echo $gtmsgs&#1111;'id'].&quote;**&quote;.$gtmsgs&#1111;'name'].&quote;**&quote;.$gtmsgs&#1111;'chattext'];
	}
}else{ // not a message request, so insert new message...burrito
	mysql_query(&quote;insert into 21_chatcli 
	(
	name,
	cliid,
	time,
	chattext,
	room
	) 
	values 
	(
	'&quote;.$_POST&#1111;'name'].&quote;',
	&quote;.$_POST&#1111;'cliid'].&quote;,
	now(),
	'&quote;.mysql_escape_string($_POST&#1111;'rtex']).&quote;',
	'&quote;.mysql_escape_string($_POST&#1111;'room']).&quote;'
	)&quote;)
		or die(&quote;**&quote;.mysql_error());;
} // end if for message request or new message...burrito
?&gt;

Code: Select all

&lt;?
////////////////////////////////////////////////////////////
//                  BreakFast Chat v1.0						
//						  Authors:							
//						  Burrito(burrito@burritostand.com) 
//						  SMPDawg							
//					Last Modified: 04/28/2005				
//					 Created: 3/12/2005						
//  Please feel free to use/distribute/modify this code		
//  I only ask that if you make any improvements that you	
//  email me the updated code so I can incorporate it into  
//  my version  burrito@burritostand.com.  I also ask 	
//  that you retain this copy © info at the top of every	
//  page of the application.								
//  enjoy!!													
//copyright © 2005 Burrito (burrito AT burritostand DOT Com)
////////////////////////////////////////////////////////////

// refresh this page every time it's hit...burrito
header(&quote;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quote;); 
header(&quote;Last-Modified: &quote; . gmdate(&quote;D, d M Y H:i:s&quote;) . &quote; GMT&quote;); 
header(&quote;Cache-Control: no-store, no-cache, must-revalidate&quote;); 
header(&quote;Cache-Control: post-check=0, pre-check=0&quote;, false); 
header(&quote;Pragma: no-cache&quote;); 
// grab db connect info....burrito
require_once(&quote;../includes/dbInteract.php&quote;);
tt_db_connect();
// grab participants in current room...burrito
$partsqu = &quote;select * from 21_chatusers where room = '&quote;.mysql_escape_string($_GET&#1111;'room']).&quote;' and lasttime &gt; '&quote;.date(&quote;Y-m-d H:i:s&quote;, strtotime(&quote;-10 seconds&quote;)).&quote;'&quote;;
$getparts = mysql_query($partsqu)
	or die(mysql_error());
	$thisamt = mysql_num_rows($getparts);
// check to see if this is initial load to return all participants...burrito
if($thisamt !== $_GET&#1111;'clist']){
	$stky = $thisamt.&quote;**&quote;;
	while($gtparts = mysql_fetch_assoc($getparts)){ // return one row at a time for new users joining...burrito
		$stky .= $gtparts&#1111;'name'].&quote;&lt;br&gt;&quote;;
	}// end while for result set...burrito
	echo $stky;
} // end if for need to send down new participant info...burrito
?&gt;
i ha. I have not thoroughly tested with FF but have had mixed experiences with it (mostly good). But it's apparent there are some that are bad which are causing it to not work at all. I know this isn't what you want to hear, but for demonstration purposes, might be better to try hitting it with IE :?

I'll let all of you geniouses work out the FF bugs
////////////////////////

I didn't want to post this just yet because it still has some bugs (especially with firefox) but after sooo many people requesting information about chat applications, I thought this could be a good resource for people looking to create their own with somethign other than java/flash and the like.

I do ask that if you modify it in any way, that you please provide me with the changes so that I can incorporate them into my version (assuming I like them :)).

it uses subqueries so 4.1 is required on the db end.

i have it set up here and if you click that link and join the chat, it will email me so I can give you a short demonstration.

I made this quite some time ago and havent' had the time/desire (more desire than time) to fix the small issues that have come up. It's a good start though and I'll bet if you put a little work and effort into it, you could make it super spicy...but remmeber to keep me posted on the spice.

Code: Select all

-- phpMyAdmin SQL Dump
-- version 2.6.1-rc2
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Mar 13, 2005 at 07:42 PM
-- Server version: 4.1.10
-- PHP Version: 5.0.3
--
-- Database: `chat`
--

-- --------------------------------------------------------

--
-- Table structure for table `chatcli`
--

DROP TABLE IF EXISTS `chatcli`;
CREATE TABLE IF NOT EXISTS `chatcli` (
  `id` mediumint(9) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `cliid` mediumint(9) default NULL,
  `time` datetime default NULL,
  `chattext` longtext,
  `room` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  KEY `time` (`time`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=382 ;

-- --------------------------------------------------------

--
-- Table structure for table `chatread`
--

DROP TABLE IF EXISTS `chatread`;
CREATE TABLE IF NOT EXISTS `chatread` (
  `id` mediumint(9) NOT NULL auto_increment,
  `chatid` mediumint(9) default NULL,
  `cliid` mediumint(9) default NULL,
  `name` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  KEY `cliid` (`cliid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=800 ;

-- --------------------------------------------------------

--
-- Table structure for table `chatusers`
--

DROP TABLE IF EXISTS `chatusers`;
CREATE TABLE IF NOT EXISTS `chatusers` (
  `id` mediumint(9) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `time` datetime default NULL,
  `lasttime` datetime default NULL,
  `room` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  KEY `room` (`room`),
  KEY `lasttime` (`lasttime`),
  KEY `time` (`time`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=53 ;

Code: Select all

&lt;?
////////////////////////////////////////////////////////////
//                  BreakFast Chat v1.0						
//						  Authors:							
//						  Burrito(burrito@burritostand.com) 
//						  SMPDawg							
//					Last Modified: 04/28/2005				
//					 Created: 3/12/2005						
//  Please feel free to use/distribute/modify this code		
//  I only ask that if you make any improvements that you	
//  email me the updated code so I can incorporate it into  
//  my version  burrito@burritostand.com.  I also ask 	
//  that you retain this copy © info at the top of every	
//  page of the application.								
//  enjoy!!													
//copyright © 2005 Burrito (burrito AT burritostand DOT Com)
////////////////////////////////////////////////////////////


require_once(&quote;../includes/dbInteract.php&quote;);
tt_db_connect();
if(isset($_GET&#1111;'invite'])){
	mysql_query(&quote;insert into 21_chatreq
	(
	req,
	userid,
	reqname,
	reqtime
	)
	values
	(
	&quote;.$_GET&#1111;'rm'].&quote;,
	&quote;.$_GET&#1111;'invite'].&quote;,
	'&quote;.mysql_escape_string($_GET&#1111;'name']).&quote;',
	now()
	)
	&quote;)
		or die(mysql_error());
}
$rm = (isset($_GET&#1111;'rm']) ? $_GET&#1111;'rm'] : &quote;General Chat&quote;);
$cliid = $_SERVER&#1111;&quote;REMOTE_ADDR&quote;];
$cliid = substr(md5($cliid), rand(0, 23), ;
$chatWho = (isset($_GET&#1111;'name']) ? $_GET&#1111;'name'] : &quote;Guest&quote;.$cliid);
$inssql = &quote;insert into 21_chatusers
(
name,
time,
lasttime,
room
)
values
(
'&quote;.mysql_escape_string($chatWho).&quote;',
now(),
now(),
'&quote;.mysql_escape_string($rm).&quote;'
)&quote;;
mysql_query($inssql)
  or die(mysql_error());
$cliid = mysql_insert_id();

?&gt;

&lt;!DOCTYPE HTML PUBLIC &quote;-//W3C//DTD HTML 4.01 Transitional//EN&quote;&gt;

&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Chat&lt;/title&gt;
&lt;style&gt;
input
{
background-color:#6F747D; border:1px #000000 solid; color:#ffffff; font-family:tahoma; font-size:8.5pt; padding-left:2px;
}
textarea.sid
{
width:400px; height:100px; background-color:#EBEADB; border:1px #000000 solid; color:#000000; font-family:tahoma; font-size:9.0pt; padding-left:2px;
}
.dsty
{
width:400px; height:400px; background-color:#EDEDE2; border:1px #000000 solid; color:#353519; overflow:auto;  font-family:tahoma; font-size:9.0pt; padding-left:2px; padding-top:2px;
}
td.le
{
border-left:1px #2E3E3F solid; border-top:1px #2E3E3F solid; border-bottom:1px #2E3E3F solid;
}
td.rt
{
border-right:1px #2E3E3F solid; border-top:1px #2E3E3F solid; border-bottom:1px #2E3E3F solid; padding-top:2px;
}
td.rb
{
border-left:1px #2E3E3F solid;
}
.parts
{
width:190px; height:270px; background-color:#68768E; border:1px #000000 solid; color:#ffffff; overflow:auto;  font-family:tahoma; font-size:8.5pt; padding-left:2px; padding-top:2px; text-align:center;
}
&lt;/style&gt;
&lt;script&gt;
function addIt(what,who,color,to){
  var clr = color;
  var temp = document.createElement(&quote;div&quote;);
  temp.style.color = clr;
  if(!document.all){
    temp.innerHTML = who + &quote;: &quote; + what;
  }else{
    temp.innerText = who + &quote;: &quote; + what;
  }
  if (temp.innerHTML); else
    temp.appendChild(document.createTextNode(what));
  if (temp.innerHTML){
    var html = temp.innerHTML;
    html = html.replace(/(\r?\n)/g, &quote;&lt;br&gt;$1&quote;);
    html = html.replace(/(&#1111;a-zA-Z_0-9.-]+\@&#1111;a-zA-Z_0-9.-]+\.\w+)/g, &quote;&lt;a href=\&quote;mailto:$1\&quote;&gt;$1&lt;/a&gt;&quote;);
    html = html.replace(new RegExp(&quote;(\\w+&#1111;\\://]+\\w+\\.\\w+\\.\\w+&#1111;/\\w+]*&#1111;.\\w+]*)&quote;, &quote;g&quote;), &quote;&lt;a href=\&quote;$1\&quote; target=\&quote;_blank\&quote;&gt;$1&lt;/a&gt;&quote;);
    html = html.replace(new RegExp(&quote;(\\\\')&quote;, &quote;g&quote;),&quote;'&quote;);
    temp.innerHTML = html;
  }
  if(to){
    sendIt();
    setTimeout(&quote;clearIt()&quote;,0);
  }
  document.getElementById(&quote;rvie&quote;).appendChild(temp);
    temp.scrollIntoView();
  document.MyForm.rtex.focus();
}

function clearIt(){
	document.MyForm.rtex.value = &quote;&quote;;
}







function checkEnt(evt){
  if(evt.keyCode == 13 &amp;&amp; !evt.shiftKey){
      addIt(document.MyForm.rtex.value,'TD','#222755','from');
    
    evt.returnValue = false;
	

    if (evt.preventDefault)
    evt.preventDefault();

  }
}

if (window.XMLHttpRequest){
  req = new XMLHttpRequest();
}else{
  req = new ActiveXObject(&quote;Microsoft.XMLHTTP&quote;);
}

if (window.XMLHttpRequest){
  reqs = new XMLHttpRequest();
}else{
  reqs = new ActiveXObject(&quote;Microsoft.XMLHTTP&quote;);
}
// SMPDawg - Added another object to send messages and (hopefully) prevent the chat
// application from trying to send/receive from one XMLHTTP object.
if (window.XMLHttpRequest){
  reqsend = new XMLHttpRequest();
}else{
  reqsend = new ActiveXObject(&quote;Microsoft.XMLHTTP&quote;);
}
whoid = 0;
function retrieveIt(simid) {
  samid = (!simid ? 0 : simid);
  datevar = new Date();
  y = datevar.getFullYear();
  m = datevar.getMonth() + 1;
  d = datevar.getDate();
  h = datevar.getHours();
  i = datevar.getMinutes();
  s = datevar.getSeconds();
  formatted = y+&quote;-&quote;+m+&quote;-&quote;+d+&quote; &quote;+h+&quote;:&quote;+i+&quote;:&quote;+s;
  req.open(&quote;GET&quote;, &quote;getchat.php?Timestr=&quote;+escape(formatted)+&quote;&amp;nm=1&amp;cliid=&lt;?=$cliid;?&gt;&amp;room=&lt;?=$rm;?&gt;&amp;rid=&quote;+samid, true);
  req.onreadystatechange = function(){
    if (req.readyState == 4 &amp;&amp; req.status == 200) {

      if(req.responseText.indexOf(&quote;**&quote;) != -1){

        whot = req.responseText.split(&quote;**&quote;);
        whoid = whot&#1111;0];
        whoto = whot&#1111;1];
        whota = whot&#1111;2];
        addIt(whota,whoto,&quote;#353519&quote;);

      }else{
        whoid = 0;
      }
    }
  }
    req.send(null);

  setTimeout(&quote;retrieveIt(whoid)&quote;,1000);
}

// SMPDawg - Changed reqs to reqsend per the previous change.
function sendIt() {
  datevar = new Date();
  y = datevar.getFullYear();
  m = datevar.getMonth() + 1;
  d = datevar.getDate();
  h = datevar.getHours();
  i = datevar.getMinutes();
  s = datevar.getSeconds();
  formatted = y+&quote;-&quote;+m+&quote;-&quote;+d+&quote; &quote;+h+&quote;:&quote;+i+&quote;:&quote;+s;
  reqsend.onreadystatechange = function(){
    if (reqsend.readyState == 4 &amp;&amp; reqsend.status == 200) {

    }
  };
  reqsend.open(&quote;POST&quote;, &quote;getchat.php&quote;, true);
  var stuff = &quote;rtex=&quote; + escape(document.MyForm.rtex.value) + &quote;&amp;name=&quote; + escape(document.MyForm.name.value) + &quote;&amp;cliid=&quote; + document.MyForm.cliid.value + &quote;&amp;room=&quote; + escape(document.MyForm.room.value);
  reqsend.setRequestHeader(&quote;Content-Type&quote;,&quote;application/x-www-form-urlencoded&quote;);
  reqsend.setRequestHeader(&quote;Content-Length&quote;,stuff.length);
  reqsend.send(stuff);


}
clist = 0;
function whosIn() {
  datevar = new Date();
  y = datevar.getFullYear();
  m = datevar.getMonth() + 1;
  d = datevar.getDate();
  h = datevar.getHours();
  i = datevar.getMinutes();
  s = datevar.getSeconds();
  formatted = y+&quote;-&quote;+m+&quote;-&quote;+d+&quote; &quote;+h+&quote;:&quote;+i+&quote;:&quote;+s;
  reqs.open(&quote;GET&quote;, &quote;getparts.php?Timestr=&quote;+escape(formatted)+&quote;&amp;cliid=&lt;?=$cliid;?&gt;&amp;room=&lt;?=$rm;?&gt;&amp;clist=&quote; + clist, true);
  reqs.onreadystatechange = function(){
    if (reqs.readyState == 4 &amp;&amp; reqs.status == 200) {

      if(reqs.responseText.indexOf(&quote;**&quote;) != -1){
        whot = reqs.responseText.split(&quote;**&quote;);
        document.getElementById('pts').innerHTML = &quote;&lt;u&gt;Chat Room&lt;/u&gt;&lt;br&gt;&quote; + whot&#1111;1];
        clist = whot&#1111;0];
      }
    }
  }
  reqs.send(null);

  setTimeout(&quote;whosIn()&quote;,1000);
}
function addPart(idd,namer){

  document.getElementById('pts').innerHTML += namer + &quote;&lt;br&gt;&quote;;
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body onLoad=&quote;retrieveIt(), whosIn(), document.MyForm.rtex.focus()&quote; topmargin=&quote;0&quote; marginheight=&quote;0&quote; leftmargin=&quote;0&quote; marginwidth=&quote;0&quote;&gt;
&lt;form name=&quote;MyForm&quote; method=&quote;post&quote;&gt;
&lt;table width=&quote;600&quote; cellspacing=&quote;0&quote; cellpadding=&quote;0&quote;&gt;
&lt;tr&gt;
&lt;td rowspan=&quote;2&quote; width=&quote;400&quote; class=&quote;le&quote;&gt;
  &lt;table width=&quote;100%&quote; cellpadding=&quote;2&quote; cellspacing=&quote;0&quote;&gt;
  &lt;tr&gt;
  &lt;td&gt;
  &lt;div id=&quote;rvie&quote; class=&quote;dsty&quote;&gt;&lt;/div&gt;

&lt;textarea name=&quote;rtex&quote; class=&quote;sid&quote; onKeyDown=&quote;checkEnt(event)&quote; &gt;&lt;/textarea&gt;&lt;br&gt;
&lt;input type=&quote;button&quote; value=&quote;Send Message&quote; onClick=&quote;addIt(document.MyForm.rtex.value,'&lt;?=$chatWho;?&gt;','#222755','from')&quote; style=&quote;width:400px; align:center&quote;&gt;

&lt;input type=&quote;hidden&quote; name=&quote;name&quote; value=&quote;&lt;?=$chatWho;?&gt;&quote;&gt;
&lt;input type=&quote;hidden&quote; name=&quote;cliid&quote; value=&quote;&lt;?=$cliid;?&gt;&quote;&gt;
&lt;input type=&quote;hidden&quote; name=&quote;room&quote; value=&quote;&lt;?=$rm;?&gt;&quote;&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;


&lt;/td&gt;
&lt;td class=&quote;rt&quote; align=&quote;center&quote; valign=&quote;top&quote; height=&quote;270&quote;&gt;
&lt;div id=&quote;pts&quote; class=&quote;parts&quote;&gt;


&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quote;rb&quote; height=&quote;230&quote;&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Code: Select all

&lt;?
////////////////////////////////////////////////////////////
//                  BreakFast Chat v1.0						
//						  Authors:							
//						  Burrito(burrito@burritostand.com) 
//						  SMPDawg							
//					Last Modified: 04/28/2005				
//					 Created: 3/12/2005						
//  Please feel free to use/distribute/modify this code		
//  I only ask that if you make any improvements that you	
//  email me the updated code so I can incorporate it into  
//  my version  burrito@burritostand.com.  I also ask 	
//  that you retain this copy © info at the top of every	
//  page of the application.								
//  enjoy!!													
//copyright © 2005 Burrito (burrito AT burritostand DOT Com)
////////////////////////////////////////////////////////////

// refresh this page every time it's hit...burrito
header(&quote;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quote;); 
header(&quote;Last-Modified: &quote; . gmdate(&quote;D, d M Y H:i:s&quote;) . &quote; GMT&quote;); 
header(&quote;Cache-Control: no-store, no-cache, must-revalidate&quote;); 
header(&quote;Cache-Control: post-check=0, pre-check=0&quote;, false); 
header(&quote;Pragma: no-cache&quote;); 
// grab db connect info....burrito
require_once(&quote;../includes/dbInteract.php&quote;);
tt_db_connect();

// check to see if this is a request for chat messages...burrito
if(isset($_GET&#1111;'nm'])){
	// update user table to show they are still logged in...burrito
	mysql_query(&quote;update 21_chatusers set lasttime = now() where id = &quote;.$_GET&#1111;'cliid'])
		or die(mysql_error());
	// check to see if need to insert a read message row...burrito
	if(isset($_GET&#1111;'rid']) &amp;&amp; $_GET&#1111;'rid'] != 0){
		$sql = &quote;insert into 21_chatread (chatid,cliid) values (&quote;.$_GET&#1111;'rid'].&quote;,&quote;.$_GET&#1111;'cliid'].&quote;)&quote;;
		mysql_query($sql)
			or die(mysql_error());
	} // end if for need to insert a read message...burrito
	$graqu = &quote;select * from 21_chatcli WHERE id NOT IN (SELECT chatid FROM 21_chatread where cliid = &quote;.$_GET&#1111;'cliid'].&quote;) and cliid &lt;&gt; &quote;.$_GET&#1111;'cliid'].&quote; and time &gt; (select time from 21_chatusers where id = &quote;.$_GET&#1111;'cliid'].&quote;) and room = '&quote;.mysql_escape_string($_GET&#1111;'room']).&quote;'&quote;;
	$getmsgs = mysql_query($graqu)
		or die(mysql_error());;
	if($gtmsgs = mysql_fetch_assoc($getmsgs)){
		// send back the chat messages that have not been read...burrito
		echo $gtmsgs&#1111;'id'].&quote;**&quote;.$gtmsgs&#1111;'name'].&quote;**&quote;.$gtmsgs&#1111;'chattext'];
	}
}else{ // not a message request, so insert new message...burrito
	mysql_query(&quote;insert into 21_chatcli 
	(
	name,
	cliid,
	time,
	chattext,
	room
	) 
	values 
	(
	'&quote;.$_POST&#1111;'name'].&quote;',
	&quote;.$_POST&#1111;'cliid'].&quote;,
	now(),
	'&quote;.mysql_escape_string($_POST&#1111;'rtex']).&quote;',
	'&quote;.mysql_escape_string($_POST&#1111;'room']).&quote;'
	)&quote;)
		or die(&quote;**&quote;.mysql_error());;
} // end if for message request or new message...burrito
?&gt;

Code: Select all

&lt;?
////////////////////////////////////////////////////////////
//                  BreakFast Chat v1.0						
//						  Authors:							
//						  Burrito(burrito@burritostand.com) 
//						  SMPDawg							
//					Last Modified: 04/28/2005				
//					 Created: 3/12/2005						
//  Please feel free to use/distribute/modify this code		
//  I only ask that if you make any improvements that you	
//  email me the updated code so I can incorporate it into  
//  my version  burrito@burritostand.com.  I also ask 	
//  that you retain this copy © info at the top of every	
//  page of the application.								
//  enjoy!!													
//copyright © 2005 Burrito (burrito AT burritostand DOT Com)
////////////////////////////////////////////////////////////

// refresh this page every time it's hit...burrito
header(&quote;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quote;); 
header(&quote;Last-Modified: &quote; . gmdate(&quote;D, d M Y H:i:s&quote;) . &quote; GMT&quote;); 
header(&quote;Cache-Control: no-store, no-cache, must-revalidate&quote;); 
header(&quote;Cache-Control: post-check=0, pre-check=0&quote;, false); 
header(&quote;Pragma: no-cache&quote;); 
// grab db connect info....burrito
require_once(&quote;../includes/dbInteract.php&quote;);
tt_db_connect();
// grab participants in current room...burrito
$partsqu = &quote;select * from 21_chatusers where room = '&quote;.mysql_escape_string($_GET&#1111;'room']).&quote;' and lasttime &gt; '&quote;.date(&quote;Y-m-d H:i:s&quote;, strtotime(&quote;-10 seconds&quote;)).&quote;'&quote;;
$getparts = mysql_query($partsqu)
	or die(mysql_error());
	$thisamt = mysql_num_rows($getparts);
// check to see if this is initial load to return all participants...burrito
if($thisamt !== $_GET&#1111;'clist']){
	$stky = $thisamt.&quote;**&quote;;
	while($gtparts = mysql_fetch_assoc($getparts)){ // return one row at a time for new users joining...burrito
		$stky .= $gtparts&#1111;'name'].&quote;&lt;br&gt;&quote;;
	}// end while for result set...burrito
	echo $stmixed experiences with it (mostly good).  But it's apparent there are some that are bad which are causing it to not work at all.  I know this isn't what you want to hear, but for demonstration purposes, might be better to try hitting it with IE   

I'll let all of you geniouses work out the FF bugs
////////////////////////

I didn't want to post this just yet because it still has some bugs (especially with firefox) but after sooo many people requesting information about chat applications, I thought this could be a good resource for people looking to create their own with somethign other than java/flash and the like.

I do ask that if you modify it in any way, that you please provide me with the changes so that I can incorporate them into my version (assuming I like them ).

it uses subqueries so 4.1 is required on the db end.

i have it set up [url=http://www.21-learn.com/chat/enterchat.php]here[/url] and if you click that link and join the chat, it will email me so I can give you a short demonstration.

I made this quite some time ago and havent' had the time/desire (more desire than time) to fix the small issues that have come up.  It's a good start though and I'll bet if you put a little work and effort into it, you could make it super spicy...but remmeber to keep me posted on the spice.

Code: Select all

-- phpMyAdmin SQL Dump
-- version 2.6.1-rc2
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Mar 13, 2005 at 07:42 PM
-- Server version: 4.1.10
-- PHP Version: 5.0.3
--
-- Database: `chat`
--

-- --------------------------------------------------------

--
-- Table structure for table `chatcli`
--

DROP TABLE IF EXISTS `chatcli`;
CREATE TABLE IF NOT EXISTS `chatcli` (
  `id` mediumint(9) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `cliid` mediumint(9) default NULL,
  `time` datetime default NULL,
  `chattext` longtext,
  `room` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  KEY `time` (`time`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=382 ;

-- --------------------------------------------------------

--
-- Table structure for table `chatread`
--

DROP TABLE IF EXISTS `chatread`;
CREATE TABLE IF NOT EXISTS `chatread` (
  `id` mediumint(9) NOT NULL auto_increment,
  `chatid` mediumint(9) default NULL,
  `cliid` mediumint(9) default NULL,
  `name` varchar(255) defau
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

I'm unaware of how I missed this... but thanks, Burrito.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Code: Select all

require_once("../includes/dbInteract.php");
Gonna need an example of that to make it work locally.
User avatar
Burrito
Spockulator
Posts: 4714
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

dbInteract.php is just a function with the mysql connection info:

ex:

Code: Select all

function tt_db_connect()
{
  if (!defined("CONNECTED"))
  {
    mysql_connect(DBHOST, DBUSER, DBPASS) 
      or die("Couldn't make database connection. Check your configuration ".mysql_error());
    mysql_select_db(DBSOURCE)
      or die("Couldn't select database. Check your configuration ".mysql_error());
    define("CONNECTED", TRUE);
  }
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

So you finally posted it eh? I'll see what I can do with Firefox (there I go, making it sound like I know what I'm doing).

What type of problems - with the JS I'd assume?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Burrito
Spockulator
Posts: 4714
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

give it a go poopy pants and find out... 8O

to sum up though, it's mostly just dropping messages and not keeping a persistent / accurate tabulation of who's in the chat at any given time...IE seems to handle this fine :?

It could be the different objects that are created for XMLHttp although I don't see why one browser would handle the objects that differently than the other...bottom line, it needs some fine tuning and further testing before I'd put it into a "production" environment.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

What i hate about XMLHTTP is that they don't support conditional get..
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

timvw wrote:What i hate about XMLHTTP is that they don't support conditional get..
You've stumbled into yet another browser inconsistency here. And for once, the dark side gets it right.

IE handles it correctly, and similar to a normal http request. It requests the data, but if the Last modified & etag matches, it doesnt transfer.

Firefox on the other hand always does a transfer - which you can't override.

There are actually a number of reasons why FF does it this way, and I disagree with all the arguments for it. Nevertheless, thats the state of affairs.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

According to http://jpspan.sourceforge.net/wiki/doku ... ttpheaders IE has it's own oddities with caching ;)
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

timvw wrote:According to http://jpspan.sourceforge.net/wiki/doku ... ttpheaders IE has it's own oddities with caching ;)
From that page:
Perhaps the general rule here is the IE implementation handles caching for you unless you start setting caching header, in which case it hands over control to your code. Mozilla expects you to handle caching yourself.
Like I said. :)
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Post by thomas777neo »

Hey Burrito

Can't you do us a favour and have your code downloadable somewhere.
Would be much appreciated. Thanks
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I've written a simple DHTML chat, which works flawlessly in all browsers that I've tested (IE, Opera, Netscape, & FF)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

I'm trying to put together a chat app for a project and its a bitch - lookin to share the knowledge scrotaye?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Basically you have two <div> layers. The chat log in one, the message form box in the other.

Then you have two hidden iframes. one to process new posted messages, another to see if there's any new messages to post to the chat log

The post page, inserts records into the database when someone submits the message box form

The other one, uses javascript to refresh every one second (javascript makes it seemless) and append the new information (if there is any) to the chat log area. This creates a perfect, non-flickering, non-refreshing, java-like DHTML chat.

If you'd like specifics, PM me, and I'll show you my code.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Here is a snippet wrote once.. (It uses http://jpspan.sf.net)
I've changed to a longer time to refresh because it's not nice to flood your webserver ;) (And XMLHTTP requests don't seem to understand http cache)

http://timvw.madoka.be/programming/php/chat_server.txt
http://timvw.madoka.be/programming/php/chat_client.txt
http://timvw.madoka.be/programming/php/chat_dump.sql
Post Reply