| px | top | add code | search | signup | login | help |
2 files to create to work..
----------------------------
Entrance for Chat
------------------
<html>
<head>
<title>-www.yoursite.com - mbChat v1.0</title>
</head>
<body bgcolor=\"black\" text=\"lime\">
<center><font size=\"5\">Enter \" Le Cafe \"</font></center><p>
<font size=3 color=\"yellow\">To enter the Chat please enter a nickname below and choose \" Enter \"</font>
<br><form action=\"/chat2.php3?\" method=\"post\">
Nickname: <input type=\"text\" name=\"NNM\" size=\"16\" maxlength=\"16\" value=\"\">
<input type=\"hidden\" name=\"message\" value=\"--- Enters the Chat ---\"><br>
<input type=\"submit\" value=\"Enter\">
</form>
</body>
</html>
-----------end-------------
What makes it work..
----------------------
Name file chat2.php3
-<?
echo \" <html><head><title>www.yoursite.com</title></head><body bgcolor=black text=lime link=red>Hi <b>$NNM</b><br><form method=post><input type=hidden name=NNM value=$NNM> <input type=text name=message size=30><p><input type=submit value=Say></form><br><center><font color=yellow>Refresh constantly to see new people entering as well as others chatting... we suggest refreshing every 10 - 15 seconds.</font><form method=post><input type=hidden name=NNM value=$NNM><input type=submit value=Refresh !!></form></center></body>\";
?>
<?
$person = str_replace (\"\\n\",\" \", $NNM);
$person = str_replace (\"<\", \" \", $NNM);
$person = str_replace (\">\", \" \", $NNM);
$person = stripslashes ($NNM);
?>
<?
/* the $chat_file_ok is a txt file (or whatever else) I use for */
/* messages storage */
$chat_file_ok = \"c:\\\\Xitami\\\\webpages\\\\msg.txt\";
/* $chat_lenght is the number of messages displayed */
$chat_lenght = 10;
/* $max_file_size is the maximum file size the msg.txt file can reach */
/* assuming that any chatter doesn\'t write a message longer than */
/* $max_single_msg_lenght (this case: 100,000 bytes = 100Kb) */
$max_single_msg_lenght = 100000;
$max_file_size = $chat_lenght * $max_single_msg_lenght;
/* check if file size is over maximum (set with $max_file_size ) */
$file_size= filesize($chat_file);
/* if file size is more than allowed then */
/* reads last $chat_lenght messages (last lines of msg.txt file) */
/* and stores them in $lines array */
/* then deletes the \"old\" msg.txt file and create a new msg.txt */
/* pushing the \"old\" messages stored in $lines array into the */
/* \"new\" msg.txt file using $msg_old. */
/* Note: this is done in order to avoid huge msg.txt file size. */
if ($file_size > $max_file_size) {
/* reads file and stores each line $lines\' array elements */
$lines = file($chat_file_ok);
/*get number of lines */
$a = count($lines);
$u = $a - $chat_lenght;
for($i = $a; $i >= $u ;$i--){
$msg_old = $lines[$i] . $msg_old;
}
$deleted = unlink($chat_file_ok);
$fp = fopen($chat_file_ok, \"a+\");
$fw = fwrite($fp, $msg_old);
fclose($fp);
}
/* the following is because every message has to be */
/* placed into one single line in the msg.txt file. */
/* You can render \\n (new lines) with \"<br>\" html tag anyway. */
$msg = str_replace (\"\\n\",\" \", $message);
/* if the user writes something... */
/* the new message is appended to the msg.txt file */
/* REMEMBER: the message is appended, hence, if */
/* you want the last message to be displayed as the */
/* first one, you have to */
/* 1. store the lines (messages) into the array */
/* 2. read the array in reverse order */
/* 3. post the messages in the output file (the chat) */
/* I added these three lines in order to avoid buggy html code and slashes */
$msg = str_replace (\"\\n\",\" \", $message);
$msg = str_replace (\"<\", \" \", $msg);
$msg = str_replace (\">\", \" \", $msg);
$msg = stripslashes ($msg);
if ($msg != \"\"){
$fp = fopen($chat_file_ok, \"a+\");
$fw = fwrite($fp, \"\\n<b>$NNM :</b> $msg<br>\");
fclose($fp);
}
$lines = file($chat_file_ok);
$a = count($lines);
$u = $a - $chat_lenght;
/* reads the array in reverse order and outputs to chat */
for($i = $a; $i >= $u ;$i--){
echo $lines[$i] . \"<hr>\";
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.