Trainer / Instructor UNIX C++ / C Senior-Hands On Programmer-WSI
WSI Nationwide, Inc.
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

PHP-Based Chat Room
Once upon a time there was a reasonably popular web-based chat room called Star Trekker chat. I happened into this chat thanks to a friend and even though Star Trek fans were hardly my favourite group of people I found that for the most part people in there were friendly and fun. But when Star Trekker shut down, thanks to its Perl backend eating server resources for lunch, these happy and kindly people were left with nowhere to go. It was fortunate that at that time I opened my own similar chat room and managed to attract much of the homeless traffic from Trekker. Wary of the resource problems caused by Perl, I was pleased when a friend introduced me to PHP.
This particular design of web-based chat uses variables posted from a form, processes them into HTML and writes them to a file. Put the form and the message file in a frameset and you have something that looks reasonably like a BeSeen chat room. Of course the advantage is, our chat room can be a little more clever than it's BeSeen cousin.
<form action="chat.php3" method="post">
Name : <input type="text" name="name"><br>
Message : <input type="text" name="message"><br>
<input type="submit" value="Send">
</form>
This is your basic form input. You'll probably want to pretty it up more than that, but to all intents and purposes, this is that you're dealing with. It sends two variables through to chat.php3 called $name and $message.
Before we deal with those variables, however, we need to extract the current contents of the message file, otherwise we'd only see one message at a time. Hardly a way to conduct a conversation. Being familiar as I am with the structure of my own message file, I know that each message is terminated by a newline character. This means I can use the file() function to read the message file into an array.
The message file is 12 lines long. Of those 12 lines, the line 1 is a set of headers, lines 2-11 are old messages and line 12 contains my footers.
All I am interested in is obtaining a string that contains most of those old messages.

<?php

// Read file into an array
$message_array = file("messages.html");

// Compile the string
for ($counter = 1; $counter < 10; $counter++) {
    
$old_messages .= $message_array[$counter];
}

?>
When compiling the string, I initiated the for loop with $counter = 1 not $counter = 0 as is common. This is because I know that element 0 of $message_array contains my headers and I don't want those. Also, by setting the loop condition to $counter < 10 means that only elements 1 thru 9 of the array are read into the string. Of the other two elements, 11 contains my footers and 10 contains the oldest message. Both of which I want to remove so I only ever have 10 messages on screen at any given time. Altering the $counter < 10 expression allows you to vary the amount of messages retained.
Now I have my old messages I want to make the new message. We have our two variables $name and $message so writing a new message string is easy.

<?php
$new_message
= "$name : $message<br>\n";
?>
[ Next Page ]


Comments:
thanxlakmal10/11/08 15:01
Problem with scriptgalucci06/20/08 03:52
Bad tutorialslpctrl05/30/07 21:56
PDF IN PHPerie05/03/07 03:05
Source filehira01/13/06 05:51
How do I create a Voice Chat in ASPMichael11/26/05 22:30
mysql chat did not work for mehary11/09/05 12:28
A MySQL Driven Chat Script hary11/05/05 19:28
PHP chat program queryJack10/13/05 10:28
RE: PHP Open New Window +Header???sabiha06/25/05 23:37
i need voice chat source code for php or asphany03/19/05 08:12
Server load and chatSam03/07/05 07:14
Ä«/µå/µ¹·Á¸·±â·Î/¸Á°¡Áö½ÅºÐ/²À º¸¼¼¿ä!ÀÌÇýÁø12/07/04 01:40
½Å.¿ë.ºÒ.·®/Ä«.µå.¿¬.ü/´ë.Ãâ/È¥ÀÚ/ÇØ.°áÇÏ´Â/¹æ.¹ý±èÇö¼­12/06/04 06:11
½Å.¿ë.ºÒ.·®.ÀÚ/´çÀÏ500/´ë.Ãâ.ºñ.¹ýÀÌÈñÁø12/05/04 04:28
´ë'Ãâ'°Å'Àý'½Ã'100%µÇ'°Ô'ÇÏ'´Â'¹æ'¹ýÇѰæ¿í12/04/04 05:32
Full chat room tutorialMitch11/30/04 14:43
½Å.¿ë.ºÒ.·®.ÀÚ/´çÀÏ500/´ë.Ãâ.ºñ.¹ýÀÌÈñÁø11/27/04 04:28
´ë'Ãâ'°Å'Àý'½Ã'100%µÇ'°Ô'ÇÏ'´Â'¹æ'¹ýÇѰæ¹Î11/23/04 05:08
½Å.¿ëºÒ.·®ÀÚ°¡ ¾Ë¾Æ¾ßÇÒ Á¤.º¸ ´ë.°ø.°³ ÀÌ´ÙÁø11/21/04 18:18
Ä«/µå/µ¹·Á¸·±â·Î/¸Á°¡Áö½ÅºÐ/²À º¸¼¼¿ä!ÀÌÁ¤¿¬11/18/04 08:50
RE: PHP Socket ServerBhuban Paudel11/16/04 04:56
Ä«.µå °ª.¿ø.±Ý.¸¸.°±.´Â.¹æ.¹ý(°­.Ãß)Áö¿µÈñ11/13/04 09:35
copyrightstu08/27/04 07:03
re: socket serverphpNewbie07/08/04 21:47
PHP Socket ServerChristian Sueiras06/27/04 10:03
No Refresh, No Java, No Flash - (just how)Zach02/29/04 00:25
Cool Implementation of Chat Room HereJohn02/08/04 03:58
You guys can get the code here, for free!Vineet Sharda11/23/03 22:41
½Å¿ëºÒ·®ÀÚ Ä«µåºúµ¶ÃË ÀüÈ­¹Þ±â ¿ä·É!(Çʵ¶)ÀÌÁöÇö11/07/03 00:36
½Å¿ëºÒ·®ÀÚ Ä«µåºûµ¶ÃË ÀüÈ­¹Þ±â ¿ä·É!!(Çʵ¶)½ÅÇý¿ø10/24/03 07:16
Smilies?Tommy10/21/03 14:30
RE: codingramkumar09/20/03 13:30
RE: Here's a much better PHP chat programMoleman04/30/03 08:39
Here's a much better PHP chat programDarren Gates03/10/03 16:46
RE: a chat which doesn't use META-REFRESHINGJake h02/22/03 23:27
RE: This whole forum looks like 1st grade Jake h02/22/03 22:51
RE: Server loadDave02/20/03 07:32
RE: This whole forum looks like 1st grade KIDDSCRIPT12/08/02 06:05
chat serverraji11/12/02 00:17
RE: FilterHTMLvellu - verkonpunojat10/23/02 01:05
RE: CHAT PROGRAMmaisya10/22/02 23:54
RE: CHAT PROGRAMgholamreza emadi10/20/02 01:48
Great tutorial - code availableB()_b10/19/02 19:56
annoying 'tik' when refreshingWim Mostrey10/10/02 10:11
RE: form input leaves the areaWim Mostrey10/09/02 10:27
RE: banning IP'sMike10/06/02 22:02
RE: banning IP'sTom Styner10/01/02 02:52
Fatal Error:Failed opening required...abhishek09/28/02 01:07
form input leaves the areaFrederick Pohl09/26/02 12:54
RE: SourceFilippo09/24/02 13:14
php variables and framesetPaolo09/21/02 10:55
PHP Code HelpShane09/18/02 11:49
RE: Works like a dream!Bix09/17/02 18:03
popup javaShawn09/07/02 02:34
number of filesDan08/30/02 21:15
Works like a dream!Bix08/30/02 08:02
What they all said... ¬_¬SotiCoto08/26/02 19:27
ErrorMitesh08/24/02 03:19
SourceTeemu Heittola08/13/02 08:14
RE: programming with phpMichael Carel08/09/02 01:50
I need help but can't understand thisLucas Diablo08/03/02 20:33
programming with phpelvayor08/03/02 10:21
*How do I start PHP in MAC OS X 10.1.3??Matt07/25/02 06:48
Learning php :)Kris07/17/02 17:11
Grateful lot aren't we? :Pmadhaha07/14/02 18:04
I need tar2rpm ...Lukas07/11/02 09:36
Anyone who needs the code: help for bradVineet Sharda07/09/02 17:48
please help!!brad07/05/02 12:47
anyone can help me???S.O.Sshrinetai07/04/02 05:11
Anyone who needs the codeVineet06/27/02 02:37
php chatluuka06/20/02 17:20
Will Pay for complete code, put together!Ryan06/16/02 06:02
DatabaseNeil Chuckervertty06/06/02 02:53
is it all one file?k-tech05/28/02 13:21
HELP ME NEED CHAT SCROLLSilence05/24/02 15:10
PHP Installation iPlanet Web serverbeth05/20/02 16:57
RE: where do i get the source codejohn05/18/02 03:20
IMAP/LDAP mail configurationPenta05/15/02 03:53
RE: where do i get the source codeScotty05/12/02 03:08
Problem: Starting with PostgreSql in Linux aslam05/09/02 14:58
where do i get the source codeabdhul04/24/02 14:33
This is the worst chat Guide ever writtenawkemacs04/23/02 12:10
RE: No script engine for file extension 'php'zakreg04/22/02 18:24
flow chartDhanesh04/15/02 11:04
How to do a log outmaria04/10/02 12:35
Linux server with postgres backendbmorion04/08/02 08:30
RE: PHP Open New Window +Header???SendMeSpam04/05/02 19:47
RE: a chat which doesn't use META-REFRESHINGMartyn Pittuck04/05/02 05:05
how display table form MySQL ?sobani03/27/02 09:22
RE: CHAT PROGRAMAldo03/24/02 10:09
RE: ?! how to use this ... joshua03/21/02 02:15
- output - what'swrong?tommi03/19/02 12:38
trim()owq03/17/02 00:21
appending new messages to the bottomGreg03/09/02 15:15
Advice ?Chris02/26/02 12:10
great tutorialA.Visser02/24/02 00:04
Chat roomNajib Maneea02/13/02 14:31
Chat rommNajib Maneea02/13/02 14:24
PHP Open New Window +Header???Kester02/13/02 11:12
I don't understand thisJay02/04/02 19:11
save to file questionBrad02/01/02 09:47
help pleaseEric01/20/02 02:48
threadGil01/13/02 13:06
Need to find tunner to get to the light.carlos01/06/02 00:40
Mac Address & modemizo12/28/01 21:29
how to run phpsrinivas12/17/01 07:04
Force Users to Type their User namesLebanonDude12/16/01 23:34
RE: How do I change the httpd user permissions?Steven Chalker12/16/01 20:33
How do I change the httpd user permissions?Daniel Razafsky12/12/01 20:52
RE: Permission deniedMike Hall12/12/01 09:33
RE: Permission deniedSteven Chalker12/11/01 16:54
Permission deniedDaniel Razafsky12/11/01 11:46
find out, who was calling a functionSebastian Poßner12/05/01 09:00
where can i show the preview of a chat room?mildream12/04/01 01:51
RE: chat programmingshahid akram12/04/01 01:05
I NEED A PHP PAINT-CHAT COMBINATIONRCDALY11/25/01 00:35
RE: web based mail like yahoo or hotmailSteven Chalker11/24/01 10:05
just wonderingAlaina11/19/01 18:52
RE: Adding to end of chatrenat11/13/01 09:47
Output bufferingnagesh11/10/01 11:56
where is the php.ini should locate?Fred11/07/01 22:52
chat server like yahoo Mohammed Abid11/07/01 11:46
RE: confrence in chat ????????Tejal11/03/01 12:24
NET SEARCH !!!! matrox10/25/01 12:52
IRC based chatIan10/24/01 12:21
Maintain quick refreshSelf Maintaining Log10/03/01 13:46
Please help meKaty09/24/01 11:52
RE: web based mail like yahoo or hotmailBruno09/21/01 09:19
web based mail like yahoo or hotmailMichael09/13/01 19:38
RE: a chat which doesn't use META-REFRESHINGMike Hall09/07/01 02:09
RE: Database driven chat vs. File drivenMike Hall09/07/01 02:04
RE: Um. Send.html. Where does it go?Mike Hall09/07/01 01:58
RE: Filtering WordsMike Hall09/07/01 01:57
RE: htmlspecialchars vs. strip_tagsMike Hall09/07/01 01:55
Why not to give full listing?Andy09/05/01 07:48
Need full sourceSteven09/04/01 14:07
Um. Send.html. Where does it go?nlafferty09/01/01 14:12
Automatic jump to web pagejch08/31/01 12:55
what is oracle?sheetal08/29/01 04:59
Filtering WordsAlex Davies08/27/01 14:32
dbmopenreza08/26/01 01:17
Download aYia-TChat hereMegalos08/20/01 14:57
a chat which doesn't use META-REFRESHINGMichael Menzel08/17/01 17:13
Problem with refreshing !!!!!!!!!Victoria Afonina08/17/01 15:02
Can i download the source code of this tutorimatant08/17/01 08:14
htmlspecialchars vs. strip_tagsGreg Kline08/15/01 11:58
RE: Adding to end of chatMerijn08/14/01 04:23
RE: banning IP'sdyfed bowen08/11/01 18:47
Adding AvatarsMacLeod08/10/01 18:21
help me plzAhmed ali08/08/01 00:36
chat programmingvikram08/03/01 09:25
programming with phpRuchi07/23/01 09:51
RE: Please help meDave Forrest07/23/01 05:02
Database driven chat vs. File drivenDave Forrest07/23/01 01:31
how to scroll up!!ice_berg1607/15/01 03:32
how to scroll up?ice_berg1607/15/01 03:29
how to scroll up?ice_berg1607/15/01 03:26
?! how to... Rony07/01/01 05:29
how to retreving querystring in next php pagevic zhou06/27/01 07:13
chat namez!!!!!!!!!!!!!!!agnes06/20/01 18:01
RE: banning IP'sMike Hall06/18/01 00:33
RE: Flow ChartMike Hall06/18/01 00:30
Manual / help filesCubud06/09/01 01:37
Cookies enabled?D. Vinaya06/06/01 03:16
Construct a chatingrid05/31/01 07:28
banning IP'srmtwede05/30/01 13:33
Flow ChartCisox05/19/01 00:26
Help me!!!Arthur05/16/01 06:31
I Need HelpJason Saggers05/10/01 03:17
Please help meLaura05/03/01 12:26
download source from here....carlos04/30/01 11:06
AuthorizationGee04/30/01 10:02
Username Listing: help please...Yury Zaytsev04/15/01 14:31
getting errorpoetic04/14/01 20:23
Someone knows how to......Christian04/14/01 19:14
file( ) is Invalid or Unsupported?Olito04/04/01 13:22
Refreshless chatting :)Tiago Bonamigo04/03/01 08:55
RE: Private messages?Tiago Bonamigo04/03/01 08:47
I'm lost.I can't seem to get the chat workingStoneneedle03/31/01 09:20
RE: Implementing Server Push Technology ?Eric Blade03/26/01 12:13
RE: Implementing Server Push Technology ?eisi03/25/01 07:55
**How to add to end of chat**Oliver03/21/01 05:10
RE: invalid function call: Advice plMoMad03/19/01 14:49
RE: FilterHTMLMoMad03/19/01 14:46
RE: Shared memory ?Dwi Wahyono03/18/01 21:58
Shared memory ?Dwi Wahyono03/16/01 03:18
RE: Private messages?Mike Hall03/15/01 04:06
Wrapper php->wapyvi03/14/01 07:03
Private messages?Leos Ondra03/07/01 12:23
Session problemYogesh Patel03/06/01 06:15
RE: Adding to end of chatMarinne Chuang02/26/01 08:25
RE: the form action and its targetGianluca RIccardi02/22/01 09:47
RE: the form action and its targetMike Hall02/22/01 05:43
the form action and its targetGianluca Riccardi02/20/01 23:03
RE: FilterHTMLMike Hall02/05/01 12:38
FilterHTMLkenneth lamb02/05/01 00:38
RE: Why not share your code with us ?Sheila Summers01/31/01 11:48
Adding to end of chatIan Rose01/30/01 10:57
RE: Exclusions in Chat TutorialMike Hall01/22/01 14:04
Exclusions in Chat TutorialSam01/21/01 20:22
RE: CHAT PROGRAMMike Hall01/20/01 17:51
RE: invalid function call: Advice plMike Hall01/20/01 17:48
RE: Server loadMike Hall01/20/01 17:44
RE: INSTALL PHP4 ON WIN 2000 NTibekowies01/17/01 18:18
INSTALL PHP4 ON WIN 2000 NTTeachingU01/15/01 14:40
RE: CHAT PROGRAMGumstic01/15/01 11:16
RE: invalid function call: Advice plGumstic01/15/01 11:13
RE: Server loadGumstic01/15/01 11:12
RE: Server loadGumstic01/15/01 11:10
RE: PHP versus ASPRemon01/09/01 09:32
REAL quick questionChris01/07/01 10:31
confrence in chat ????????senthil velan01/04/01 02:34
RE: Why not share your code with us ?Mike Hall01/03/01 04:35
chat room + confirmation mail Champak Chakraborty01/03/01 04:32
RE: Why not share your code with us ?Greg Militelo01/02/01 16:29
RE: Why not share your code with us ?Mike Hall12/31/00 08:38
*brought this up but got no response*Stoney12/30/00 17:27
Why not share your code with us ?Philip Degasi12/30/00 12:28
RE: Username ListingMike Hall12/27/00 17:38
Username ListingEvan12/22/00 11:20
PHP versus ASPalan dooly12/20/00 11:14
RE: The complete Script?Mike Hall12/19/00 09:20
RE: messages.htmlMike Hall12/19/00 09:20
The complete Script?BtB12/14/00 14:11
messages.htmlEvan12/11/00 09:31
.php3 extensions.Stoney11/24/00 00:21
Chat ServerFabio Kaminski11/23/00 13:56
help please!petra11/13/00 23:14
RE: refreshing chat.php3 - locking names inRyan Murphy - iglooevents.com11/13/00 17:31
refreshing chat.php3jan robert10/30/00 13:51
questionronal chandra09/27/00 03:40
RE: Implementing Server Push Technology ?Mike Hall09/19/00 09:32
RE: Reverse messagesMichael Lykfeldt09/11/00 10:33
RE: Reverse messagesMichael Lykfeldt09/11/00 10:01
RE: Implementing Server Push Technology ?Frank HEflin09/08/00 17:35
RE: Reverse messagesMike Hall09/05/00 05:58
RE: Reverse messagesMichael Lykfeldt09/03/00 06:24
RE: Reverse messagesMike Hall08/30/00 06:15
Hidden Frame doesn't work !Lee08/27/00 21:37
RE: Reverse messagesCharles Capps08/22/00 06:00
Reverse messagesMichael Lykfeldt08/17/00 06:06
RE: Implementing Server Push Technology ?Charles Capps08/17/00 05:21
RE: Implementing Server Push Technology ?Marcel Beerta08/13/00 10:13
php could it beG. Anderson07/29/00 22:14
RE: Implementing Server Push Technology ?Michael Kaiser07/18/00 08:29
RE: Server loadMike Hall07/11/00 12:52
RE: Server loadAntonio Gallo07/11/00 11:36
RE: Implementing Server Push Technology ?Luis Argerich07/10/00 16:06
RE: Implementing Server Push Technology ?Mike Hall07/10/00 05:43
RE: Implementing Server Push Technology ?Alain Jiang07/09/00 21:28
A point of noteMike Hall07/08/00 05:21
RE: Implementing Server Push Technology ?Mike Hall07/08/00 05:18
Implementing Server Push Technology ?Alain Jiang07/08/00 04:59
RE: CHAT PROGRAMMike Hall07/07/00 06:42
CHAT PROGRAMAmita Jalla07/07/00 05:09
RE: Server loadCharles Capps07/06/00 08:28
RE: invalid function call: Advice plMike Hall07/06/00 06:45
RE: invalid function call: Advice plMike Hall07/06/00 06:36
RE: invalid function call: Advice plAmita Jalla07/06/00 05:53
RE: Server loadMike Hall07/06/00 04:49
RE: Server loadElvis Presley07/05/00 16:30
RE: invalid function call: Advice plMike Hall07/05/00 10:13
RE: invalid function call: Advice plAmita Jalla07/05/00 05:06
RE: invalid function call: Advice plMike Hall07/04/00 06:34
invalid function call: Advice plAmita Jalla07/04/00 06:09
RE: Please HelpAmita Jalla07/03/00 23:46
RE: Server loadMike Hall07/03/00 08:46
RE: Please HelpMike Hall07/03/00 08:44
Please HelpAmita Jalla07/03/00 06:33
RE: Not so goodCharles Capps07/02/00 19:33
Not so goodleandro07/02/00 17:54
codingAmita Jalla07/01/00 05:45
RE: Server loadLuis Argerich06/30/00 16:35
RE: Server loadMike Hall06/29/00 08:14
RE: Server loadCharles Capps06/29/00 07:56
RE: Server loadMike Hall06/28/00 05:19
Server loadBill06/27/00 07:31
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.