Zend - The PHP Company




Chat

Add Code


mini chat system  

Type: code fragment
Added by: fabricet
Entered: 01/08/2000
Last modified: 08/12/1999
Rating: **** (7 votes)
Views: 19706
A mini chat system, to be customized.


<form name="bbs">
<?php
// set your vars here
$h="localhost";$d="dtb";$u="user";$p="password";
mysql_connect($h,$u,$p);

// if user typed some word and press enter
if ($message) {
// for lame flood proof purpose only
$ip getenv("REMOTE_ADDR"); $do gethostbyaddr($ip); $br =  getenv("HTTP_USER_AGENT");
// 1 query possible?
$q "SELECT MIN(dh) FROM bbs";
$s mysql_db_query($d,$q); while ( $r mysql_fetch_array($s) )
$mindh $r["MIN(dh)"];}
$q "UPDATE bbs SET ip='$ip',do='$do',br='$br',msg='$message',dh=now() WHERE dh LIKE '$mindh'";
mysql_query($q);
mysql_free_result($s);
}

$q "SELECT * FROM bbs ORDER BY dh";
$s mysql_db_query($d,$q);
while ( 
$r mysql_fetch_array($s) )
$dh $r["dh"]; $ip $r["ip"]; $do $r["do"]; $br $r["br"]; $msg $r["msg"];$id $r["id"];

$tablo split(" "$dh2);
// we keep only hour:minutes
ereg"([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})"$tablo[1], $regs );
$hm "$regs[1].$regs[2]";

// javascript-enabled client required
echo "<a href="#" OnClick="alert(document.bbs.flood$id.value)">$hm</a> $msg<br>";
echo "<input type="hidden" name="flood$id" value="$do ($ip) | $br $da">";
}
mysql_free_result($s);
?>
<input type="text" name="message" size="16"></form>


Usage Example


1. create the table with values(required):
CREATE TABLE bbs (
ip varchar(15),
do varchar(80),
br varchar(120),
msg varchar(255),
dh datetime,
id tinyint(5) unsigned NOT NULL auto_increment,
KEY id (id)
);

# table 'bbs'
INSERT INTO bbs VALUES( '192.168.0.3', 'framboise', 'Mozilla', 'test', '1996-01-19 02:28:10', '1');
INSERT INTO bbs VALUES( '192.168.0.3', 'framboise', 'Mozilla', 'test', '1996-01-19 02:28:02', '2');
INSERT INTO bbs VALUES( '192.168.0.3', 'framboise', 'Mozilla', 'test', '1996-01-19 02:28:04', '3');
INSERT INTO bbs VALUES( '192.168.0.3', 'framboise', 'Mozilla', 'test', '1996-01-19 02:28:04', '4');

2. alter the vars: user, password etc.

3. put the form above into your page.

some advices:
- Insert more lines of values for more entries.
- dont fill the action="" property in the <form> tag and dont rename the form (the alert() js method won't work)
- please check before print $msg for security reasons
- mail me any improvement cause it really sucks.
original idea: http://ojuice.planet-d.net


Rate This Script





Search



This Category All Categories