Chatbox
Auteur: barry - 24 december 2005 - 19:21 - Gekeurd door: nemesiskoen - Hits: 13889 - Aantal punten: 4.32 (14 stemmen)
Hier een chatbox geprogrammeerd met de AJAX-technologie (Asynchronous JavaScript and XML).
De installatie werkt als volgd:
Maak de volgende tabellen aan in een MySQL database:
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(18) collate latin1_general_ci NOT NULL default '',
`date` decimal(25,0) NOT NULL default '0',
`color` varchar(7) collate latin1_general_ci NOT NULL default '#000000',
PRIMARY KEY (`id`),
KEY `name` (`name`,`date`),
KEY `color` (`color`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=403 ;
CREATE TABLE `users` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(18) COLLATE latin1_general_ci NOT NULL DEFAULT '', `date` DECIMAL(25,0) NOT NULL DEFAULT '0', `color` VARCHAR(7) COLLATE latin1_general_ci NOT NULL DEFAULT '#000000', PRIMARY KEY (`id`), KEY `name` (`name`,`date`), KEY `color` (`color`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=403 ;
CREATE TABLE `msgs` (
`id` int(11) NOT NULL auto_increment,
`who` varchar(18) collate latin1_general_ci NOT NULL default '',
`color` varchar(7) collate latin1_general_ci NOT NULL default '#000000',
`msg` varchar(255) collate latin1_general_ci NOT NULL default '',
`type` enum('msg','info','logoff','logon') collate latin1_general_ci NOT NULL default 'msg',
`date` decimal(25,0) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `who` (`who`,`color`,`msg`,`type`,`date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=2967 ;
CREATE TABLE `msgs` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `who` VARCHAR(18) COLLATE latin1_general_ci NOT NULL DEFAULT '', `color` VARCHAR(7) COLLATE latin1_general_ci NOT NULL DEFAULT '#000000', `msg` VARCHAR(255) COLLATE latin1_general_ci NOT NULL DEFAULT '', `type` ENUM('msg','info','logoff','logon') COLLATE latin1_general_ci NOT NULL DEFAULT 'msg', `date` DECIMAL(25,0) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `who` (`who`,`color`,`msg`,`type`,`date`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=2967 ;
De enige benodigde bestanden zijn index.php en server.php.
Ook heeft het script de optie om smileys te gebruiken, hiervoor kun je de smileys downloaden van: http://files.dsound.nl/smileys.rar
Het script heeft wel zijn mankementen,
in sommige browsers zal het lijken voor de gebruiker dat zijn berichten dubbel gepost worden, dit zien anderen echter niet.
Ook, omdat het script feitelijk constant server.php opnieuw inlaad, zullen sommige browsers in de statusbalk aangeven dat de website nog geladen wordt.
Verder is het een goed werken script, maar onzeker is of dit wel de ideale manier is om een chatbox te maken.
AJAX biedt namelijk veel meer mogelijkheden met zijn functionaliteiten, denk maar aan Googlemaps en Gmail, welke ook met de AJAX-technologie zijn geprogrammeerd.
Allereerst hier mijn chatbox:
|
Code: |
index.php
<?
session_start();
mysql_connect("localhost", "****", "****");
mysql_select_db("****");
function authUser($name) {
mysql_query("INSERT INTO `users` VALUES ('', '".$name."', '".mktime()."', '#000000 ');");
$r = mysql_query("SELECT * FROM `users` WHERE `name` = '".$name."'");
$a = mysql_fetch_array($r, MYSQL_ASSOC);
$_SESSION['userId'] = $a['id'];
$_SESSION['name'] = $name;
}
if ($_POST['username']) {
$r = mysql_query("SELECT * FROM `users` WHERE `name` = '".$_POST['username']."'");
if (mysql_num_rows($r) == 0) {
authUser($_POST['username']);
}
else {
header("location: index.php?msg=UserExists");
}
}
?>
<html>
<head>
<title>Chatbox<?=($_SESSION['name']) ? ' ('.$_SESSION['name'].')' : '';?></title>
<style type="text/css">
body, td, th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #000000;
}
a:link, a:visited, a:active {
color: #333333;
text-decoration: underline;
}
a:hover {
text-decoration: none;
color: #003399;
}
#chatframe, #chatters {
height: 384px;
background-color: #DEDEDE;
border: 1px #000000 solid;
overflow: auto;
text-align: left;
padding: 2px;
word-spacing: inherit;
}
input {
background-color: #EEEEEE;
border: 1px #000000 solid;
height: 22px;
}
#chatbox {
border: 1px #000000 solid;
background-color: #B5AE9B;
}
#chatbox td {
vertical-align: top;
text-align: left;
padding: 8px;
}
</style>
<script>
<?
echo ($_SESSION['name']) ? 'timertje = setTimeout("chat()",400)'."\n" : '';
?>
function xmlHttpGo(div, url) {
var w3c = (typeof window.XMLHttpRequest != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');
if (w3c) {
var xmlHttp = new XMLHttpRequest();
}
else if (ie) {
var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
return false;
}
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
alertRoot(xmlHttp);
}
}
xmlHttp.send(null);
function alertRoot(xmlHttp) {
if (div == 'chatters') {
var txt = xmlHttp.responseText
document.getElementById(div).innerHTML = txt;
}
else {
var txt = xmlHttp.responseText
var lastxt = document.getElementById("last");
if (txt != lastxt.value) {
document.getElementById(div).innerHTML += txt;
}
lastxt.value = txt;
}
}
}
function users() {
var d = new Date();
url = 'server.php?get=users&random='+d.getFullYear()+(d.getMonth()+1)+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds()
xmlHttpGo('chatters', url, false)
document.getElementById('chatframe').scrollTop = 10000000;
window.setTimeout("users()",2000)
}
function chatadd(type) {
var d = new Date();
var randomU = d.getFullYear()+(d.getMonth()+1)+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds()
if (type == 'logon') {
var url = 'server.php?msg=&type=logon&u=<?=$_SESSION['userId'];?>&random='+randomU
}
else if (type == 'logoff') {
var url = 'server.php?msg=&type=logoff&u=<?=$_SESSION['userId'];?>&random='+randomU
}
else {
var url = 'server.php?msg='+document.getElementById("msg").value+'&type=msg&u=<?=$_SESSION['userId'];?>&random='+randomU
}
xmlHttpGo('chatframe', url)
document.getElementById("msg").value = '';
document.getElementById('chatframe').scrollTop = 10000000;
clearTimeout(timertje)
chat();
return (false);
}
function chat() {
var d = new Date();
var randomU = d.getFullYear()+(d.getMonth()+1)+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds()
xmlHttpGo('chatframe', 'server.php?msg=&random='+randomU)
timertje = setTimeout("chat()",400)
document.getElementById('chatframe').scrollTop = 10000000;
window.status = 'Chatbox...';
}
</script>
</head>
<?
if ($_SESSION['name']) {
echo '<body onLoad="chatadd(\'logon\'); users();" onUnload="chatadd(\'logoff\');">';
}
else {
echo '<body>';
}
?>
<center>
<table id="chatbox" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="528">
<pre id="chatframe" style="width: 512px;"></pre>
<?
if ($_SESSION['userId']) {
echo ' <form name=frm onSubmit="return chatadd(\'msg\');">
<input id="msg" type="text" name="msg" style="width: 512px;">
<input type="hidden" name="last" id="last" value="">
</form>';
}
else {
if ($_GET['msg'] == 'UserExists') {
$msg = ' <br><b>Deze gebruikersnaam wordt al gebruikt... Kies een ander a.u.b.</b>';
}
echo ' Please get a username first!<br>
<form method=post action="">
Naam: <input type="text" name="username">
<input type="submit" name="submit" value="Ok">
'.$msg.'
</form>';
}
?>
</td>
<td width="144">
<pre id="chatters" style="width: 128px;"></pre>
</td>
</tr>
</table>
</center>
</body>
</html>
<? function authUser($name) { mysql_query("INSERT INTO `users` VALUES ('', '".$name."', '".mktime()."', '#000000 ');"); $r = mysql_query("SELECT * FROM `users` WHERE `name` = '".$name."'"); $_SESSION['userId'] = $a['id']; $_SESSION['name'] = $name; } if ($_POST['username']) { $r = mysql_query("SELECT * FROM `users` WHERE `name` = '".$_POST['username']."'"); authUser($_POST['username']); } else { header("location: index.php?msg=UserExists"); } } ?> <html> <head> <title>Chatbox<?=($_SESSION['name']) ? ' ('.$_SESSION['name'].')' : '';?></title> <style type="text/css"> body, td, th { font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #000000; } a:link, a:visited, a:active { color: #333333; text-decoration: underline; } a:hover { text-decoration: none; color: #003399; } #chatframe, #chatters { height: 384px; background-color: #DEDEDE; border: 1px #000000 solid; overflow: auto; text-align: left; padding: 2px; word-spacing: inherit; } input { background-color: #EEEEEE; border: 1px #000000 solid; height: 22px; } #chatbox { border: 1px #000000 solid; background-color: #B5AE9B; } #chatbox td { vertical-align: top; text-align: left; padding: 8px; } </style> <script> <? echo ($_SESSION['name']) ? 'timertje = setTimeout("chat()",400)'."\n" : ''; ?> function xmlHttpGo(div, url) { var w3c = (typeof window.XMLHttpRequest != 'undefined'); var ie = (typeof window.ActiveXObject != 'undefined'); if (w3c) { var xmlHttp = new XMLHttpRequest(); } else if (ie) { var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } else { return false; } xmlHttp.open("GET", url, true); xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { alertRoot(xmlHttp); } } xmlHttp.send(null); function alertRoot(xmlHttp) { if (div == 'chatters') { var txt = xmlHttp.responseText document.getElementById(div).innerHTML = txt; } else { var txt = xmlHttp.responseText var lastxt = document.getElementById("last"); if (txt != lastxt.value) { document.getElementById(div).innerHTML += txt; } lastxt.value = txt; } } } function users() { var d = new Date(); url = 'server.php?get=users&random='+d.getFullYear()+(d.getMonth()+1)+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds() xmlHttpGo('chatters', url, false) document.getElementById('chatframe').scrollTop = 10000000; window.setTimeout("users()",2000) } function chatadd(type) { var d = new Date(); var randomU = d.getFullYear()+(d.getMonth()+1)+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds() if (type == 'logon') { var url = 'server.php?msg=&type=logon&u=<?=$_SESSION['userId'];?>&random='+randomU } else if (type == 'logoff') { var url = 'server.php?msg=&type=logoff&u=<?=$_SESSION['userId'];?>&random='+randomU } else { var url = 'server.php?msg='+document.getElementById("msg").value+'&type=msg&u=<?=$_SESSION['userId'];?>&random='+randomU } xmlHttpGo('chatframe', url) document.getElementById("msg").value = ''; document.getElementById('chatframe').scrollTop = 10000000; clearTimeout(timertje) chat(); return (false); } function chat() { var d = new Date(); var randomU = d.getFullYear()+(d.getMonth()+1)+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds() xmlHttpGo('chatframe', 'server.php?msg=&random='+randomU) timertje = setTimeout("chat()",400) document.getElementById('chatframe').scrollTop = 10000000; window.status = 'Chatbox...'; } </script> </head> <? if ($_SESSION['name']) { echo '<body onLoad="chatadd(\'logon\ '); users();" onUnload="chatadd(\'logoff\ ');">'; } else { } ?> <center> <table id="chatbox" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="528"> <pre id="chatframe" style="width: 512px;"></pre> <? if ($_SESSION['userId']) { echo ' <form name=frm onSubmit="return chatadd(\'msg\ ');"> <input id="msg" type="text" name="msg" style="width: 512px;"> <input type="hidden" name="last" id="last" value=""> </form>'; } else { if ($_GET['msg'] == 'UserExists') { $msg = ' <br><b>Deze gebruikersnaam wordt al gebruikt... Kies een ander a.u.b.</b>'; } echo ' Please get a username first!<br> <form method=post action=""> Naam: <input type="text" name="username"> <input type="submit" name="submit" value="Ok"> '.$msg.' </form>'; } ?> </td> <td width="144"> <pre id="chatters" style="width: 128px;"></pre> </td> </tr> </table> </center> </body> </html>
server.php
<?
session_start();
mysql_connect("localhost", "****", "****");
mysql_select_db("****");
define('smileyDir', 'smileys/');
define('maxMsgsinDB', 70);
define('delAmmountDB', 5);
define('maxIdleTime', 600);
function smiley_array() {
$smiley[] = array('code' => '(n)', 'image' => 'thumbdown.gif');
$smiley[] = array('code' => '(N)', 'image' => 'thumbdown.gif');
$smiley[] = array('code' => '(y)', 'image' => 'thumbup.gif');
$smiley[] = array('code' => '(Y)', 'image' => 'thumbup.gif');
$smiley[] = array('code' => ':(', 'image' => 'unhappy.gif');
$smiley[] = array('code' => ':-(', 'image' => 'unhappy.gif');
$smiley[] = array('code' => ':)', 'image' => 'standard.gif');
$smiley[] = array('code' => ':-)', 'image' => 'standard.gif');
$smiley[] = array('code' => '(k)', 'image' => 'kiss.gif');
$smiley[] = array('code' => '(K)', 'image' => 'kiss.gif');
$smiley[] = array('code' => '(l)', 'image' => 'love.gif');
$smiley[] = array('code' => '(L)', 'image' => 'love.gif');
$smiley[] = array('code' => ':lief:', 'image' => 'love.gif');
$smiley[] = array('code' => ':flower:', 'image' => 'flower.gif');
$smiley[] = array('code' => '(f)', 'image' => 'flower.gif');
$smiley[] = array('code' => '(F)', 'image' => 'flower.gif');
$smiley[] = array('code' => ':s', 'image' => 'confused.gif');
$smiley[] = array('code' => ':-s', 'image' => 'confused.gif');
$smiley[] = array('code' => ':S', 'image' => 'confused.gif');
$smiley[] = array('code' => ':-S', 'image' => 'confused.gif');
$smiley[] = array('code' => ':\\', 'image' => 'hmmz_streep.gif');
$smiley[] = array('code' => ':-\\', 'image' => 'hmmz_streep.gif');
$smiley[] = array('code' => ':/', 'image' => 'hmmzz_streep.gif');
$smiley[] = array('code' => ':-/', 'image' => 'hmmzz_streep.gif');
$smiley[] = array('code' => ':knuffel:','image' => 'hug.gif');
$smiley[] = array('code' => ':kusje:', 'image' => 'kiss_2.gif');
$smiley[] = array('code' => ':kus:', 'image' => 'kiss_1.gif');
$smiley[] = array('code' => 'O:)', 'image' => 'hypocrite.gif');
$smiley[] = array('code' => 'O:-)', 'image' => 'hypocrite.gif');
$smiley[] = array('code' => ':p', 'image' => 'tongue.gif');
$smiley[] = array('code' => ':-p', 'image' => 'tongue.gif');
$smiley[] = array('code' => ':P', 'image' => 'tongue.gif');
$smiley[] = array('code' => ':-P', 'image' => 'tongue.gif');
$smiley[] = array('code' => ';p', 'image' => 'tongue_wink.gif');
$smiley[] = array('code' => ';-p', 'image' => 'tongue_wink.gif');
$smiley[] = array('code' => ';P', 'image' => 'tongue_wink.gif');
$smiley[] = array('code' => ';-P', 'image' => 'tongue_wink.gif');
$smiley[] = array('code' => ':d', 'image' => 'wide.gif');
$smiley[] = array('code' => ':-d', 'image' => 'wide.gif');
$smiley[] = array('code' => ':D', 'image' => 'wide.gif');
$smiley[] = array('code' => ':-D', 'image' => 'wide.gif');
$smiley[] = array('code' => ':x', 'image' => 'shutup.gif');
$smiley[] = array('code' => ':-x', 'image' => 'shutup.gif');
$smiley[] = array('code' => ':X', 'image' => 'shutup.gif');
$smiley[] = array('code' => ':-X', 'image' => 'shutup.gif');
$smiley[] = array('code' => ':z', 'image' => 'sleepy.gif');
$smiley[] = array('code' => ':-z', 'image' => 'sleepy.gif');
$smiley[] = array('code' => ':Z', 'image' => 'sleepy.gif');
$smiley[] = array('code' => ':-Z', 'image' => 'sleepy.gif');
$smiley[] = array('code' => '(s)', 'image' => 'sleepy.gif');
$smiley[] = array('code' => '(S)', 'image' => 'sleepy.gif');
$smiley[] = array('code' => '(b)', 'image' => 'beer.gif');
$smiley[] = array('code' => '(B)', 'image' => 'beer.gif');
$smiley[] = array('code' => ':k', 'image' => 'drool.gif');
$smiley[] = array('code' => ':-k', 'image' => 'drool.gif');
$smiley[] = array('code' => ':K', 'image' => 'drool.gif');
$smiley[] = array('code' => ':-K', 'image' => 'drool.gif');
$smiley[] = array('code' => ':kwijl:', 'image' => 'drool.gif');
$smiley[] = array('code' => ':o', 'image' => 'ooo.gif');
$smiley[] = array('code' => ':-o', 'image' => 'ooo.gif');
$smiley[] = array('code' => ':O', 'image' => 'ooo.gif');
$smiley[] = array('code' => ':-O', 'image' => 'ooo.gif');
$smiley[] = array('code' => ':$', 'image' => 'bloos.gif');
$smiley[] = array('code' => ':-$', 'image' => 'bloos.gif');
$smiley[] = array('code' => ':bloos:', 'image' => 'bloos.gif');
$smiley[] = array('code' => ':gaap:', 'image' => 'yawn.gif');
$smiley[] = array('code' => ':aai:', 'image' => 'pet.gif');
$smiley[] = array('code' => ':woet:', 'image' => 'poowpoow.gif');
$smiley[] = array('code' => '(h)', 'image' => 'cool.gif');
$smiley[] = array('code' => '(H)', 'image' => 'cool.gif');
$smiley[] = array('code' => ':@', 'image' => 'angry.gif');
$smiley[] = array('code' => ':-@', 'image' => 'angry.gif');
$smiley[] = array('code' => ':respect:','image' => 'worshippy.gif');
$smiley[] = array('code' => '_o_', 'image' => 'worshippy.gif');
$smiley[] = array('code' => '_O_', 'image' => 'worshippy.gif');
$smiley[] = array('code' => ';)', 'image' => 'wink.gif');
$smiley[] = array('code' => ';-)', 'image' => 'wink.gif');
$smiley[] = array('code' => 'O:)', 'image' => 'hypocrite.gif');
$smiley[] = array('code' => 'O:-)', 'image' => 'hypocrite.gif');
$smiley[] = array('code' => 'o:)', 'image' => 'hypocrite.gif');
$smiley[] = array('code' => 'o:-)', 'image' => 'hypocrite.gif');
$smiley[] = array('code' => '8)', 'image' => 'veryhappy.gif');
$smiley[] = array('code' => '8-)', 'image' => 'veryhappy.gif');
$smiley[] = array('code' => ':|', 'image' => 'hmmz.gif');
$smiley[] = array('code' => ':-|', 'image' => 'hmmz.gif');
$smiley[] = array('code' => ':9', 'image' => 'yummie.gif');
$smiley[] = array('code' => ':-9', 'image' => 'yummie.gif');
$smiley[] = array('code' => ':ja:', 'image' => 'yes_new.gif');
$smiley[] = array('code' => ':nee:', 'image' => 'nooo.gif');
$smiley[] = array('code' => ':yes:', 'image' => 'yes_new.gif');
$smiley[] = array('code' => ':no:', 'image' => 'nooo.gif');
$smiley[] = array('code' => ":'(", 'image' => 'cry.gif');
$smiley[] = array('code' => ':frusty:', 'image' => 'frusty.gif');
return $smiley;
}
if ($_GET['get'] == 'users') {
$r = mysql_query("SELECT *FROM `users` ORDER BY `name` ASC") or die (mysql_error());
while ($a = mysql_fetch_array($r, MYSQL_ASSOC)) {
if ($a['name'] == $_SESSION['name']) {
echo '<b>'.$a['name'].'</b><br>';
}
else {
echo $a['name'].'<br>';
}
}
$status = status($_SESSION['name']);
echo '<br> <br> <br> <br><center>Status: <b>'.$status.'</b></center>';
exit;
}
function deletefirst($max, $del=1) {
$r = mysql_query("SELECT * FROM `msgs`");
$n = mysql_num_rows($r);
if ($n >= $max) {
$q = mysql_query("SELECT `id` FROM `msgs` ORDER BY `id` LIMIT ".$del."");
while ($a = mysql_fetch_array($q, MYSQL_ASSOC)) {
mysql_query("DELETE FROM `msgs` WHERE `id` = '".$a['id']."'");
}
}
}
function status($username) {
$d = mktime();
$c = $d - maxIdleTime;
$r = mysql_query("SELECT `date` FROM `users` WHERE `name` = '".$username."'");
$a = mysql_fetch_array($r, MYSQL_ASSOC);
if (mysql_num_rows($r) == 1) {
return 'online';
}
elseif ($a['date'] < $c) {
return 'afwezig';
}
else {
return 'offline';
}
}
function remove($user) {
$d = mktime();
$c = $d - maxIdleTime;
$r = mysql_query("SELECT * FROM `users` WHERE `name` = '".$_SESSION['name']."' AND `date` < '".$c."'");
if (mysql_num_rows($r) == 1) {
addMsgInDb($_SESSION['name'], '<font color="#3B0B4F"><b>Niet meer aanwezig op de chat</b></font>', '#000000', 'msg');
}
mysql_query("DELETE FROM `users` WHERE `name` = '".$user."' OR `date` < '".$c."'") or die (mysql_error());
}
function update($user) {
mysql_query("UPDATE `users` SET `date` = '".mktime()."' WHERE `name` = '".$user."'");
}
function add($user) {
$r = mysql_query("SELECT * FROM `users` WHERE `name` = '".$user."'");
if (mysql_num_rows($r) == 0) {
mysql_query("INSERT INTO `users` VALUES ('', '".$user."', '".mktime()."', '#000000 ');");
$r = mysql_query("SELECT * FROM `users` WHERE `name` = '".$user."'");
$a = mysql_fetch_array($r, MYSQL_ASSOC);
$_SESSION['userId'] = $a['id'];
$_SESSION['name'] = $user;
}
}
function replace_smileys($txt) {
$ntxt = $txt;
$arr = smiley_array();
foreach ($arr as $key => $value) {
$ntxt = str_replace($value['code'], '<img src="'.smileyDir.$value['image'].'" title="'.$value['code'].'" border=0>', $ntxt);
}
return $ntxt;
}
function addMsgInDb($who, $msg, $color, $type) {
mysql_query("INSERT INTO `msgs` VALUES ('',
'".$who."',
'".$color."',
'".$msg."',
'".$type."',
'".mktime()."');") or die (mysql_error());
}
function returnMsgsFromDb($id) {
deletefirst(maxMsgsinDB, delAmmountDB);
$r = mysql_query("SELECT * FROM `msgs` WHERE `id` >= '".$id."' ORDER BY `id` DESC LIMIT 1");
if (mysql_num_rows($r) == 0) {
return '';
}
else {
while($a = mysql_fetch_array($r, MYSQL_ASSOC)) {
if ($a['type'] == 'logon') {
$return .= '<font color="#07368D"><b>'.$a['who'].' komt de chatroom binnen</b></font><br>';
}
elseif ($a['type'] == 'logoff') {
remove($a['who']);
$return .= '<font color="#07368D"><b>'.$a['who'].' verlaat de chatroom</b></font><br>';
}
else {
$newTxt = replace_smileys($a['msg']);
if (substr($newTxt, 0, 1) == '/') {
if (substr($a['msg'], 0, 4) == '/me ') {
$return .= '<font color="#9D4168">'.date("H:i:s", $a['date']).' <b>'.$a['who'].' '.str_replace("/me ", "", $newTxt).'</b></font><br>';
}
elseif (substr($newTxt, 0, 3) == '/b ') {
$return .= date("H:i:s", $a['date']).' <b>'.$a['who'].': <font color="'.$a['color'].'">'.str_replace("/b ", "", $newTxt).'</font></b><br>';
}
elseif (substr($newTxt, 0, 3) == '/u ') {
$return .= date("H:i:s", $a['date']).' <b>'.$a['who'].'</b>: <u><font color="'.$a['color'].'">'.str_replace("/u ", "", $newTxt).'</font></u><br>';
}
elseif (substr($newTxt, 0, 3) == '/i ') {
$return .= date("H:i:s", $a['date']).' <b>'.$a['who'].'</b>: <i><font color="'.$a['color'].'">'.str_replace("/i ", "", $newTxt).'</font></i><br>';
}
elseif (substr($newTxt, 0, 8) == '/noicon ') {
$return .= date("H:i:s", $a['date']).' <b>'.$a['who'].'</b>: <font color="'.$a['color'].'">'.str_replace("/noicon ", "", $newTxt).'</font><br>';
}
else {
$return .= date("H:i:s", $a['date']).' <b>'.$a['who'].'</b>: <font color="'.$a['color'].'">'.str_replace("/", "", $newTxt).'</font><br>';
}
}
else {
$return .= date("H:i:s", $a['date']).' <b>'.$a['who'].'</b>: <font color="'.$a['color'].'">'.$newTxt.'</font><br>';
}
}
}
return $return;
}
}
$r = mysql_query("SELECT * FROM `msgs` ORDER BY `id` DESC LIMIT 1");
$a = mysql_fetch_array($r, MYSQL_ASSOC);
$_SESSION['lastId'] = $a['id'];
if ($_GET['type'] == 'logon') {
add($_SESSION['name']);
addMsgInDb($_SESSION['name'], $_GET['msg'], '#000000', $type=$_GET['type']);
}
elseif ($_GET['type'] == 'logoff') {
remove($_SESSION['name']);
addMsgInDb($_SESSION['name'], $_GET['msg'], '#000000', $type=$_GET['type']);
}
elseif ($_GET['type'] == 'msg') {
add($_SESSION['name']);
update($_SESSION['name']);
addMsgInDb($_SESSION['name'], strip_tags($_GET['msg']), '#000000', $type=$_GET['type']);
}
if ($_SESSION['userId']) {
remove(md5('dezenaamwordttochnietgebruikt!!'));
echo returnMsgsFromDb($_GET['li']);
}
else {
echo '<b>Bob</b>: No name set!<br>';
}
?>
<? define('smileyDir', 'smileys/'); function smiley_array() { $smiley[] = array('code' => '(n)', 'image' => 'thumbdown.gif'); $smiley[] = array('code' => '(N)', 'image' => 'thumbdown.gif'); $smiley[] = array('code' => '(y)', 'image' => 'thumbup.gif'); $smiley[] = array('code' => '(Y)', 'image' => 'thumbup.gif'); $smiley[] = array('code' => ':(', 'image' => 'unhappy.gif'); $smiley[] = array('code' => ':-(', 'image' => 'unhappy.gif'); $smiley[] = array('code' => ':)', 'image' => 'standard.gif'); $smiley[] = array('code' => ':-)', 'image' => 'standard.gif'); $smiley[] = array('code' => '(k)', 'image' => 'kiss.gif'); $smiley[] = array('code' => '(K)', 'image' => 'kiss.gif'); $smiley[] = array('code' => '(l)', 'image' => 'love.gif'); $smiley[] = array('code' => '(L)', 'image' => 'love.gif'); $smiley[] = array('code' => ':lief:', 'image' => 'love.gif'); $smiley[] = array('code' => ':flower:', 'image' => 'flower.gif'); $smiley[] = array('code' => '(f)', 'image' => 'flower.gif'); $smiley[] = array('code' => '(F)', 'image' => 'flower.gif'); $smiley[] = array('code' => ':s', 'image' => 'confused.gif'); $smiley[] = array('code' => ':-s', 'image' => 'confused.gif'); $smiley[] = array('code' => ':S', 'image' => 'confused.gif'); $smiley[] = array('code' => ':-S', 'image' => 'confused.gif'); $smiley[] = array('code' => ':\\', 'image' => 'hmmz_streep.gif'); $smiley[] = array('code' => ':-\\', 'image' => 'hmmz_streep.gif'); $smiley[] = array('code' => ':/', 'image' => 'hmmzz_streep.gif'); $smiley[] = array('code' => ':-/', 'image' => 'hmmzz_streep.gif'); $smiley[] = array('code' => ':knuffel:','image' => 'hug.gif'); $smiley[] = array('code' => ':kusje:', 'image' => 'kiss_2.gif'); $smiley[] = array('code' => ':kus:', 'image' => 'kiss_1.gif'); $smiley[] = array('code' => 'O:)', 'image' => 'hypocrite.gif'); $smiley[] = array('code' => 'O:-)', 'image' => 'hypocrite.gif'); $smiley[] = array('code' => ':p', 'image' => 'tongue.gif'); $smiley[] = array('code' => ':-p', 'image' => 'tongue.gif'); $smiley[] = array('code' => ':P', 'image' => 'tongue.gif'); $smiley[] = array('code' => ':-P', 'image' => 'tongue.gif'); $smiley[] = array('code' => ';p', 'image' => 'tongue_wink.gif'); $smiley[] = array('code' => ';-p', 'image' => 'tongue_wink.gif'); $smiley[] = array('code' => ';P', 'image' => 'tongue_wink.gif'); $smiley[] = array('code' => ';-P', 'image' => 'tongue_wink.gif'); $smiley[] = array('code' => ':d', 'image' => 'wide.gif'); $smiley[] = array('code' => ':-d', 'image' => 'wide.gif'); $smiley[] = array('code' => ':D', 'image' => 'wide.gif'); $smiley[] = array('code' => ':-D', 'image' => 'wide.gif'); $smiley[] = array('code' => ':x', 'image' => 'shutup.gif'); $smiley[] = array('code' => ':-x', 'image' => 'shutup.gif'); $smiley[] = array('code' => ':X', 'image' => 'shutup.gif'); $smiley[] = array('code' => ':-X', 'image' => 'shutup.gif'); $smiley[] = array('code' => ':z', 'image' => 'sleepy.gif'); $smiley[] = array('code' => ':-z', 'image' => 'sleepy.gif'); $smiley[] = array('code' => ':Z', 'image' => 'sleepy.gif'); $smiley[] = array('code' => ':-Z', 'image' => 'sleepy.gif'); $smiley[] = array('code' => '(s)', 'image' => 'sleepy.gif'); $smiley[] = array('code' => '(S)', 'image' => 'sleepy.gif'); $smiley[] = array('code' => '(b)', 'image' => 'beer.gif'); $smiley[] = array('code' => '(B)', 'image' => 'beer.gif'); $smiley[] = array('code' => ':k', 'image' => 'drool.gif'); $smiley[] = array('code' => ':-k', 'image' => 'drool.gif'); $smiley[] = array('code' => ':K', 'image' => 'drool.gif'); $smiley[] = array('code' => ':-K', 'image' => 'drool.gif'); $smiley[] = array('code' => ':kwijl:', 'image' => 'drool.gif'); $smiley[] = array('code' => ':o', 'image' => 'ooo.gif'); $smiley[] = array('code' => ':-o', 'image' => 'ooo.gif'); $smiley[] = array('code' => ':O', 'image' => 'ooo.gif'); $smiley[] = array('code' => ':-O', 'image' => 'ooo.gif'); $smiley[] = array('code' => ':$', 'image' => 'bloos.gif'); $smiley[] = array('code' => ':-$', 'image' => 'bloos.gif'); $smiley[] = array('code' => ':bloos:', 'image' => 'bloos.gif'); $smiley[] = array('code' => ':gaap:', 'image' => 'yawn.gif'); $smiley[] = array('code' => ':aai:', 'image' => 'pet.gif'); $smiley[] = array('code' => ':woet:', 'image' => 'poowpoow.gif'); $smiley[] = array('code' => '(h)', 'image' => 'cool.gif'); $smiley[] = array('code' => '(H)', 'image' => 'cool.gif'); $smiley[] = array('code' => ':@', 'image' => 'angry.gif'); $smiley[] = array('code' => ':-@', 'image' => 'angry.gif'); $smiley[] = array('code' => ':respect:','image' => 'worshippy.gif'); $smiley[] = array('code' => '_o_', 'image' => 'worshippy.gif'); $smiley[] = array('code' => '_O_', 'image' => 'worshippy.gif'); $smiley[] = array('code' => ';)', 'image' => 'wink.gif'); $smiley[] = array('code' => ';-)', 'image' => 'wink.gif'); $smiley[] = array('code' => 'O:)', 'image' => 'hypocrite.gif'); $smiley[] = array('code' => 'O:-)', 'image' => 'hypocrite.gif'); $smiley[] = array('code' => 'o:)', 'image' => 'hypocrite.gif'); $smiley[] = array('code' => 'o:-)', 'image' => 'hypocrite.gif'); $smiley[] = array('code' => '8)', 'image' => 'veryhappy.gif'); $smiley[] = array('code' => '8-)', 'image' => 'veryhappy.gif'); $smiley[] = array('code' => ':|', 'image' => 'hmmz.gif'); $smiley[] = array('code' => ':-|', 'image' => 'hmmz.gif'); $smiley[] = array('code' => ':9', 'image' => 'yummie.gif'); $smiley[] = array('code' => ':-9', 'image' => 'yummie.gif'); $smiley[] = array('code' => ':ja:', 'image' => 'yes_new.gif'); $smiley[] = array('code' => ':nee:', 'image' => 'nooo.gif'); $smiley[] = array('code' => ':yes:', 'image' => 'yes_new.gif'); $smiley[] = array('code' => ':no:', 'image' => 'nooo.gif'); $smiley[] = array('code' => ":'(", 'image' => 'cry.gif'); $smiley[] = array('code' => ':frusty:', 'image' => 'frusty.gif'); return $smiley; } if ($_GET['get'] == 'users') { if ($a['name'] == $_SESSION['name']) { echo '<b>'.$a['name'].'</b><br>'; } else { } } $status = status($_SESSION['name']); echo '<br> <br> <br> <br><center>Status: <b>'.$status.'</b></center>'; } function deletefirst($max, $del=1) { if ($n >= $max) { $q = mysql_query("SELECT `id` FROM `msgs` ORDER BY `id` LIMIT ".$del.""); mysql_query("DELETE FROM `msgs` WHERE `id` = '".$a['id']."'"); } } } function status($username) { $c = $d - maxIdleTime; $r = mysql_query("SELECT `date` FROM `users` WHERE `name` = '".$username."'"); return 'online'; } elseif ($a['date'] < $c) { return 'afwezig'; } else { return 'offline'; } } function remove($user) { $c = $d - maxIdleTime; $r = mysql_query("SELECT * FROM `users` WHERE `name` = '".$_SESSION['name']."' AND `date` < '".$c."'"); addMsgInDb($_SESSION['name'], '<font color="#3B0B4F"><b>Niet meer aanwezig op de chat</b></font>', '#000000', 'msg'); } } function update($user) { mysql_query("UPDATE `users` SET `date` = '".mktime()."' WHERE `name` = '".$user."'"); } function add($user) { $r = mysql_query("SELECT * FROM `users` WHERE `name` = '".$user."'"); mysql_query("INSERT INTO `users` VALUES ('', '".$user."', '".mktime()."', '#000000 ');"); $r = mysql_query("SELECT * FROM `users` WHERE `name` = '".$user."'"); $_SESSION['userId'] = $a['id']; $_SESSION['name'] = $user; } } function replace_smileys($txt) { $ntxt = $txt; $arr = smiley_array(); foreach ($arr as $key => $value) { $ntxt = str_replace($value['code'], '<img src="'.smileyDir .$value['image'].'" title="'.$value['code'].'" border=0>', $ntxt); } return $ntxt; } function addMsgInDb($who, $msg, $color, $type) { '".$who."', '".$color."', '".$msg."', '".$type."', } function returnMsgsFromDb($id) { deletefirst(maxMsgsinDB, delAmmountDB); $r = mysql_query("SELECT * FROM `msgs` WHERE `id` >= '".$id."' ORDER BY `id` DESC LIMIT 1"); return ''; } else { if ($a['type'] == 'logon') { $return .= '<font color="#07368D"><b>'.$a['who'].' komt de chatroom binnen</b></font><br>'; } elseif ($a['type'] == 'logoff') { remove($a['who']); $return .= '<font color="#07368D"><b>'.$a['who'].' verlaat de chatroom</b></font><br>'; } else { $newTxt = replace_smileys($a['msg']); if (substr($newTxt, 0, 1) == '/') { if (substr($a['msg'], 0, 4) == '/me ') { $return .= '<font color="#9D4168">'.date("H:i:s", $a['date']).' <b>'.$a['who'].' '.str_replace("/me ", "", $newTxt).'</b></font><br>'; } elseif (substr($newTxt, 0, 3) == '/b ') { $return .= date("H:i:s", $a['date']).' <b>'.$a['who'].': <font color="'.$a['color'].'">'.str_replace("/b ", "", $newTxt).'</font></b><br>'; } elseif (substr($newTxt, 0, 3) == '/u ') { $return .= date("H:i:s", $a['date']).' <b>'.$a['who'].'</b>: <u><font color="'.$a['color'].'">'.str_replace("/u ", "", $newTxt).'</font></u><br>'; } elseif (substr($newTxt, 0, 3) == '/i ') { $return .= date("H:i:s", $a['date']).' <b>'.$a['who'].'</b>: <i><font color="'.$a['color'].'">'.str_replace("/i ", "", $newTxt).'</font></i><br>'; } elseif (substr($newTxt, 0, 8) == '/noicon ') { $return .= date("H:i:s", $a['date']).' <b>'.$a['who'].'</b>: <font color="'.$a['color'].'">'.str_replace("/noicon ", "", $newTxt).'</font><br>'; } else { $return .= date("H:i:s", $a['date']).' <b>'.$a['who'].'</b>: <font color="'.$a['color'].'">'.str_replace("/", "", $newTxt).'</font><br>'; } } else { $return .= date("H:i:s", $a['date']).' <b>'.$a['who'].'</b>: <font color="'.$a['color'].'">'.$newTxt.'</font><br>'; } } } return $return; } } $r = mysql_query("SELECT * FROM `msgs` ORDER BY `id` DESC LIMIT 1"); $_SESSION['lastId'] = $a['id']; if ($_GET['type'] == 'logon') { add($_SESSION['name']); addMsgInDb($_SESSION['name'], $_GET['msg'], '#000000', $type=$_GET['type']); } elseif ($_GET['type'] == 'logoff') { remove($_SESSION['name']); addMsgInDb($_SESSION['name'], $_GET['msg'], '#000000', $type=$_GET['type']); } elseif ($_GET['type'] == 'msg') { add($_SESSION['name']); update($_SESSION['name']); addMsgInDb ($_SESSION['name'], strip_tags($_GET['msg']), '#000000', $type=$_GET['type']); } if ($_SESSION['userId']) { remove (md5('dezenaamwordttochnietgebruikt!!')); echo returnMsgsFromDb ($_GET['li']); } else { echo '<b>Bob</b>: No name set!<br>'; } ?>
Bovenin server.php zijn er wat constanten te veranderen, voor een functionaliteit naar wens.
Let hierbij ook de url naar de smileys. Download code (.txt)
|
|
|
Stemmen |
Niet ingelogd. |
|