So I got a chat script the other day and ?have tried playing around with it and I love how simple it is for ajax requesting. But it doesn't do database at all. It is read from a .txt file when it tries to receive what people posted. And I don't like it...
I tried to switch it to mysql request but I have failed horribly. So I came here...
The index file containing the chat box itself.
<?php
/*Connection to MY database*/
include("../connect.php");
/*Security measures*/
if(!isset($_COOKIE['member_id']))
die('Not logged in, please <a href=../index.php>go back</a>');
$Player=$_COOKIE['member_id'];
setcookie('member_id',$Player,time()+60*60*24);
$Query="SELECT * from Users where ID='$Player'";
$Query2=mysql_query($Query) or die("Could not get user stats");
$User=mysql_fetch_array($Query2);
if($_COOKIE['password'] !=$User['Password'])
die('Your Password doesnt seem right for this account. <a href=../index.php>go back</a>');
?>
<html>
<head>
<style type="text/css">
body { padding-left:40px; background:#57767F; font-family:arial;}
input, textarea { font-family: arial; color:white; background:#57767F; font-size: 14px; }
#content { width:800px; text-align:left; margin-left:60px; }
#chatwindow { border:1px solid #aaaaaa; padding:4px; background:#232D2F; color:white; width:600px; height:900px; font-family:courier new;}
#chatmsg { border: none; border-bottom:1px solid #aaaaaa; padding:4px; background:#57767F; }
</style>
</head>
<body>
<div id="content">
<?php
echo"<input id=\"chatnick\" type=\"hidden\" value=$User[Playername]>";
?>
<input id="chatmsg" type="text" size="60" maxlength="80" onkeyup="keyup(event.keyCode);">
<input type="button" value="add" onclick="submit_msg();" style="cursor:pointer;border:1px solid gray;"><br><br>
<br>
<!--Chat box-->
<p id="chatwindow"> </p>
<!-- <div id="chatwindow" rows="19" cols="95" readonly><br>
-->
</div>
</body>
</html>
<script type="text/javascript">
/****************************************************************
* Most Simple Ajax Chat Script (www.linuxuser.at) *
* Version: 3.1 *
* *
* Author: Chris (chris[at]linuxuser.at) *
* Contributors: Derek, BlueScreenJunky (http://forums.linuxuser.at/viewtopic.php?f=6&t=17)
* *
* Licence: GPLv2 *
****************************************************************/
/* Settings you might want to define */
var waittime=800;
/* Internal Variables & Stuff */
chatmsg.focus()
document.getElementById("chatwindow").innerHTML = "loading...";
var xmlhttp = false;
var xmlhttp2 = false;
/* Request for Reading the Chat Content */
function ajax_read(url) {
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
if(xmlhttp.overrideMimeType){
xmlhttp.overrideMimeType('text/xml');
}
} else if(window.ActiveXObject){
try{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch(e){
}
}
}
if(!xmlhttp) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
document.getElementById("chatwindow").innerHTML = xmlhttp.responseText;
zeit = new Date();
ms = (zeit.getHours() * 24 * 60 * 1000) + (zeit.getMinutes() * 60 * 1000) + (zeit.getSeconds() * 1000) + zeit.getMilliseconds();
intUpdate = setTimeout("ajax_read('chat.txt?x=" + ms + "')", waittime)
}
}
xmlhttp.open('GET',url,true);
xmlhttp.send(null);
}
/* Request for Writing the Message */
function ajax_write(url){
if(window.XMLHttpRequest){
xmlhttp2=new XMLHttpRequest();
if(xmlhttp2.overrideMimeType){
xmlhttp2.overrideMimeType('text/xml');
}
} else if(window.ActiveXObject){
try{
xmlhttp2=new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try{
xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
} catch(e){
}
}
}
if(!xmlhttp2) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
xmlhttp2.open('GET',url,true);
xmlhttp2.send(null);
}
/* Submit the Message */
function submit_msg(){
nick = document.getElementById("chatnick").value;
msg = document.getElementById("chatmsg").value;
document.getElementById("chatmsg").value = "";
ajax_write("w.php?m=" + msg + "&n=" + nick);
}
/* Check if Enter is pressed */
function keyup(arg1) {
if (arg1 == 13) submit_msg();
}
/* Start the Requests! ;) <-- also where I believe where I would stick the mysql query */
var intUpdate = setTimeout("ajax_read('chat.txt')", waittime);
</script>
The file containing the functions
<?php
include("../connect.php");
if(!isset($_COOKIE['member_id']))
die('Not logged in, please <a href=../index.php>go back</a>');
$Player=$_COOKIE['member_id'];
setcookie('member_id',$Player,time()+60*60*24);
$Query="SELECT * from Users where ID='$Player'";
$Query2=mysql_query($Query) or die("Could not get user stats");
$User=mysql_fetch_array($Query2);
if($_COOKIE['password'] !=$User['Password'])
die('Your Password doesnt seem right for this account. <a href=../index.php>go back</a>');
$fn = "chat.txt";
$maxlines = 20;
/* Set this to a minimum wait time between posts (in sec) */
$waittime_sec = 0;
/* spam keywords */
$spam[] = "******";
$spam[] = "###";
$spam[] = "###";
$spam[] = "###";
/* IP's to block */
$blockip[] = "72.60.167.89";
/* spam, if message IS exactly that string */
$espam[] = "ajax";
/* Get Message & Nick from the Request and Escape them */
$msg = $_REQUEST["m"];
$msg = htmlentities(stripslashes($msg));
$n = $_REQUEST["n"];
if ($waittime_sec > 0) {
$lastvisit = $_COOKIE["lachatlv"];
setcookie("lachatlv", time());
if ($lastvisit != "") {
$diff = time() - $lastvisit;
if ($diff < 5) { die(); }
}
}
if ($msg != "") {
if (strlen($msg) < 2) { die(); }
if (strlen($msg) > 3) {
/* Smilies are ok */
if (strtoupper($msg) == $msg) { die(); }
}
if (strlen($msg) > 150) { die(); }
if (strlen($msg) > 15) {
if (substr_count($msg, substr($msg, 6, 8)) > 1) { die(); }
}
foreach ($blockip as $a) {
if ($_SERVER["REMOTE_ADDR"] == $a) { die(); }
}
$mystring = strtoupper($msg);
foreach ($spam as $a) {
if (strpos($mystring, strtoupper($a)) === false) {
/* Everything Ok Here */
} else {
die();
}
}
foreach ($espam as $a) {
if (strtoupper($msg) == strtoupper($a)) { die(); }
}
$handle = fopen ($fn, 'r');
$chattext = fread($handle, filesize($fn)); fclose($handle);
$arr1 = explode("\n", $chattext);
if (count($arr1) > $maxlines) {
/* Pruning */
$arr1 = array_reverse($arr1);
for ($i=0; $i<$maxlines; $i++) { $arr2[$i] = $arr1[$i]; }
$arr2 = array_reverse($arr2);
} else {
$arr2 = $arr1;
}
$chattext = implode("\n", $arr2);
if (substr_count($chattext, $msg) > 2) { die(); }
$out = $chattext . "(" . $User['Gender'] . ")" . "<" . $User['ID'] . ">" . $n . " : " . $msg . "<br>\n";
$out = str_replace("\'", "'", $out);
$out = str_replace("\\\"", "\"", $out);
/*Where I believe I would stick the mysql query again*/
$handle = fopen ($fn, 'w'); fwrite ($handle, $out); fclose($handle);
}
?>
The database it is sending information to
CREATE TABLE IF NOT EXISTS `chat` (
`id` int(11) NOT NULL auto_increment,
`owner` varchar(30) NOT NULL default '0',
`guild` varchar(35) NOT NULL default '',
`user` text NOT NULL,
`chat` text NOT NULL,
`viewer` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
Hope you can help =)