How to Build a Simple Chat Application with PHP
Do you have an online support system with which you'd like to be able to enable live chat? Well, you can either pay for a service hosted by a 3rd party, or you could easily just build one yourself unsing PHP. This simple chat system will take user input from an HTML form, store it in a text file, and display it on the screen.
Save the code below as chat.php. Also, you'll need to create a writeable text file called msg.txt, in the same directory.
There is code included to
- strip HTML markup
- set the max number of chat messages displayed. the default is 10
- set a maximum filesize for msg.txt (default is about 100Kb). When it hits the maximum, it archives the old messages to a past message text file.
$person = str_replace ("\n"," ", $person);
$person = str_replace ("<", " ", $person); //take out HTML tags
$person = str_replace (">", " ", $person);
$person = stripslashes ($person);
?>
<form action="chat.php" method="post">
Nickname: <input type="text" name="person" size="40" maxlength="80" value="<? echo $person; ?>"><br>
Your message: <textarea name="message" rows="3" cols="40"></textarea>
<input type="submit" value="Send/refresh">
</form>
<?
$chat_file_ok = "./msg.txt";
$chat_length = 10;
$max_single_msg_length = 100000;
$max_file_size = $chat_length * $max_single_msg_length;
$file_size= filesize($chat_file);
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);
}
// 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
$msg = str_replace ("\n"," ", $message);
// if the user writes something the new message is appended to the msg.txt file
// strip 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>$person :</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>";
}
?>

on August 24th, 2006 at 1:02 am
hey can i do a complete chat application with the above code??????
im very much new to php…….
can u help
on August 24th, 2006 at 9:02 am
You might want to check out PHPmychat for that
on May 9th, 2007 at 5:57 am
It's a nice code
on August 1st, 2008 at 11:54 pm
Hi this Niki and i have got this Script can you tell me that what is all it need to be a simple Chat script on it and can you give me some idea only how it is working can you do that HELP for me
on September 29th, 2008 at 12:06 pm
I Think It Would Be Much Faster If It Used Sqlite3 Or MySQL, Instead Of An ASCII Text File. Also How Can I Take This Simple Script, And Turn It Into A Realtime Chat System, I Guess What I Mean Is, How Do You Load Something And Save Something, after the page has loaded with php?I Don't Think You Can, So You Will Either Need Flash, Or JavaScript, Or Java(Don't Use Java, It Is Super-Duper-Uber Gay!), Or CGI, Or Python(Probably The Easiest!).