⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch19.htm

📁 CGI programming is the hottest stuff to look out for in this book
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>

<HEAD>
   <TITLE>Chapter 19 -- Chat Rooms </TITLE>
   <META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 19</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>Chat Rooms</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>

<UL>
<LI><A HREF="#ChatRoomsGettingaLifeontheInterne" >Chat Rooms-Getting a Life on the Internet</A>
<LI><A HREF="#PrescriptionforaChatRoom" >Prescription for a Chat Room</A>
<LI><A HREF="#GettingchatcgitoWorkforYou" >Getting chat.cgi to Work for You</A>
<LI><A HREF="#ChatRoomSystemsandEntryPages" >Chat Room Systems and Entry Pages</A>
<LI><A HREF="#ExtensiontotheBasicChatRoom" >Extension to the Basic Chat Room</A>
<LI><A HREF="#IntelligentUserIdentification" >Intelligent User Identification</A>
<LI><A HREF="#ChatMasterTheChatRoomAdministrator" >ChatMaster-The Chat Room Administrator</A>
<LI><A HREF="#PrivateMessaging" >Private Messaging</A>
<UL>
<LI><A HREF="#OtherChatRoomFeaturesandExamples" >Other Chat Room Features and Examples</A>
</UL>
<LI><A HREF="#AlternativestoCGIChatRooms" >Alternatives to CGI Chat Rooms</A>
<UL>
<LI><A HREF="#IRCInternetRelayChat" >IRC-Internet Relay Chat</A>
<LI><A HREF="#PowWow" >PowWow</A>
<LI><A HREF="#JavaChatRooms" >Java Chat Rooms</A>
<LI><A HREF="#talkandytalkOldUNIXStandbys" >talk and ytalk-Old UNIX Standbys</A>
<LI><A HREF="#MUDMUSHandMOOSystems" >MUD, MUSH, and MOO Systems</A>
</UL>
<LI><A HREF="#Summary" >Summary</A>
</UL>
<HR>
<P>
Web-based chat rooms are one of the biggest success stories on
the Internet. Though better Internet &quot;chat technology&quot;
exists, the widespread availability of Web browsers and their
ease of use has made Web chatting a remarkable phenomenon. Special
CGI programming issues exist when creating chat rooms, including
user tracking, maintaining state, and multiple access serving.
<H2><A NAME="ChatRoomsGettingaLifeontheInterne"><FONT SIZE=5 COLOR=#FF0000>Chat
Rooms-Getting a Life on the Internet</FONT></A></H2>
<P>
I have been living a life on-line in one form or another since
1990, and chat rooms are the reason why. I'm not the only one
who has discovered the incredible appeal and even addictive quality
of on-line chat rooms. Sysadmins across the world fret and fume
about the amount of server activity chat rooms bring. People's
lives crumble around them as they fritter away dozens of hours
a week within them. Maladjusted misfits taunt other chat room
users and maliciously lie for the cheap thrill of hurting other
people. Strangers who &quot;meet&quot; in chat rooms often end
up spending obscene amounts of money in long-distance phone calls
and even plane fare on each other. On balance, chat rooms are
fairly destructive creations. And now, I'm going to teach you
how to make one.
<H2><A NAME="PrescriptionforaChatRoom"><FONT SIZE=5 COLOR=#FF0000>Prescription
for a Chat Room</FONT></A></H2>
<P>
A user's flowchart for how to use a chat room is a fairly simple,
feedback-oriented process:
<UL>
<LI><FONT COLOR=#000000>Access the chat room by entering its URL.</FONT>
<LI><FONT COLOR=#000000>Provide the chat room with a &quot;handle.&quot;
This could be your real name, but more often it's just something
you make up. (If ever you find an entity in a chat room claiming
to be &quot;Mabelrode,&quot; you might just be talking to me....)</FONT>
<LI><FONT COLOR=#000000>Type your comments into a </FONT><TT><FONT FACE="Courier">TEXTAREA</FONT></TT>
box with a form and submit that form to the chat room CGI program.
<LI><FONT COLOR=#000000>The submission enters your comments into
a &quot;stack&quot; of previous comments, and the CGI program
outputs the updated stack to your Web browser, plus a form in
which to enter new comments.</FONT>
</UL>
<P>
The last step is repeated until you're tired of chatting, likely
many hours later. Of course, this procedure is made interesting
by the fact that others are doing the same. In the time span between
typing in new comments and having your browser reload the chat
room following submission of those comments, other people have
done the same. Once the reload is done, you get to read what other
people have newly typed into the room. This loop of activity can
simulate near-real-time &quot;chatting.&quot; Figure 19.1 is a
quick view of London Chat, one of my chat rooms.
<P>
<A HREF="f19-1.gif" ><B>Figure 19.1: </B><I>A view of London Chat room writen by the author.</I></A>
<P>
The CGI programmer responsible for a chat room must take into
account the cycle I stated previously. An appreciation of the
way a chat room is used will lead to a better chat room design.
In addition to making the chat room &quot;user-friendly,&quot;
there are several organization issues that have to be addressed:
<UL>
<LI><FONT COLOR=#000000>After the user accesses the page for the
first time, the chat room program must know that it must output
a form appropriate to that user upon submission. That is, once
the user types in his name the first time, he shouldn't need to
type it in each subsequent time</FONT>.
<LI><FONT COLOR=#000000>The stack of previous postings shouldn't
be allowed to grow indefinitely large.</FONT>
<LI><FONT COLOR=#000000>Users should be provided with a way of
being able to tell how recent the postings in the stack are.</FONT>
<LI><FONT COLOR=#000000>The programmer must decide whether or
not to allow HTML within the room. Consider this: If users are
asked to submit their postings via a </FONT><TT><FONT FACE="Courier">textarea</FONT></TT>
within a form, if the output procedure is to simply regurgitate
those submissions, any <TT><FONT FACE="Courier">&lt;HTML TAGS&gt;</FONT></TT>
provided in those submissions will, by default, be interpreted
as HTML. Is this desirable? If not, the programmer must take steps
to avoid this potential problem.
</UL>
<P>
These points only scratch the surface of how a chat room should
be organized. There are other reasons for the chat room to need
to &quot;know&quot; who a given user is than just to save the
user the trouble of typing their name in repeatedly. A knowledge
of the state of the stack can be used in more ways than just limiting
its growth. Time stamps on messages can be used in other ways,
as well. Also, it may be useful to allow certain users the capability
to enter HTML into their posts while not others. I'll explore
these concepts later in this chapter.
<P>
Listing 19.1 is the source code of a chat room that addresses
the simplest level of user and programmer issues I mentioned earlier.
This program is alive and active at
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><A HREF="http://www.anadas.com/cgiunleashed/chatrooms/chat.cgi</FONT></TT>
</BLOCKQUOTE>">http://www.anadas.com/cgiunleashed/chatrooms/chat.cgi</FONT></TT>
</BLOCKQUOTE></A>
<HR>
<BLOCKQUOTE>
<B>Listing 19.1. <FONT SIZE=2 FACE="MCPdigital-B">chat.cgi</FONT>-A
functional, functioning chat room.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">#!/usr/bin/perl<BR>
<BR>
#<BR>
# This program was written by Richard Dice of Anadas Software
Development<BR>
# as part of the Sams Net &quot;CGI Programming Unleashed&quot;
book.&nbsp;&nbsp;The author<BR>
# intends this code to be used for instructional purposes and
not for<BR>
# resale or commercial gain.<BR>
#<BR>
# Any questions or comments regarding this program are welcome.&nbsp;&nbsp;You
<BR>
# may contact the author by Internet email: rdice@anadas.com<BR>
#<BR>
<BR>
# puts all POST query information the variable $input_line<BR>
read(stdin, $input_line, $ENV{CONTENT_LENGTH});<BR>
<BR>
# replace all '+' coded spaces with real spaces<BR>
$input_line =~ tr/+/ /;<BR>
<BR>
# creates array of all data files in $input_line from &amp; separated
info<BR>
@fields = split(/\&amp;/,$input_line);<BR>
<BR>
#<BR>
# decodes hex info for each name/value pair and places pairs in
<BR>
# %input associative array<BR>
#<BR>
foreach $i (0 .. $#fields) {<BR>
&nbsp;&nbsp;&nbsp;($name,$value) = split(/=/,$fields[$i]);<BR>
&nbsp;&nbsp;&nbsp;$name =~ s/%(..)/pack(&quot;c&quot;,hex($1))/ge;
<BR>
&nbsp;&nbsp;&nbsp;$value =~ s/%(..)/pack(&quot;c&quot;,hex($1))/ge;
<BR>
&nbsp;&nbsp;&nbsp;$input{$name} = $value;<BR>
}<BR>
<BR>
#<BR>
# I put a few of the CGI environment variables in their own variables
<BR>
# for ease of understanding later on in the program.&nbsp;&nbsp;also,
I create<BR>
# a variable $time which records the current time and date<BR>
#<BR>
$refer = $ENV{HTTP_REFERER};<BR>
$ip = $ENV{REMOTE_ADDR};<BR>
$browsername = $ENV{HTTP_USER_AGENT};<BR>
chop($time = `date`);<BR>
<BR>
# ====== Configuration Variables ======<BR>
$progname = 'chat.cgi#Comments';<BR>
$baseurl = 'http://www.anadas.com/cgiunleashed/chatrooms';<BR>
$html = 0; # set to 1 if HTML is allowed in postings<BR>
$maxlines = 100; # sets the size of the stack<BR>
$admin_name = 'Richard Dice';<BR>
$admin_email = 'rdice@anadas.com';<BR>
<BR>
print &quot;Content-type: text/html\n\n&quot;;<BR>
<BR>
#<BR>
# the following line traps forms being submitted from invalid
locations<BR>
#<BR>
&amp;print_error if(&nbsp;&nbsp;(!($refer =~ /$baseurl/)) &amp;&amp;
defined(%input) );<BR>
<BR>
&amp;remove_html if !($html);<BR>
<BR>
&amp;print_header;<BR>
&amp;print_form;<BR>
&amp;update_stack if $input{'comments'} ne '';<BR>
&amp;print_stack;<BR>
&amp;print_footer;<BR>
<BR>
exit 0;<BR>
<BR>
sub print_error {<BR>
&nbsp;&nbsp;&nbsp;print &lt;&lt;END;<BR>
&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Invalid Submission&lt;/TITLE&gt;&lt;/HEAD&gt;
<BR>
&lt;BODY&gt;<BR>
&lt;P&gt;<BR>
The URL of the page which submitted the form which has this CGI
program<BR>
as its action was not valid.&nbsp;&nbsp;Please go to &lt;A HREF=$refer&gt;$refer&lt;/A&gt;
to<BR>
legally access this CGI program.<BR>
&lt;/BODY&gt;&lt;/HTML&gt;<BR>
END<BR>
&nbsp;&nbsp;&nbsp;exit 1;<BR>
}<BR>
<BR>
sub remove_html {<BR>
<BR>
#<BR>
# removes all characters between &lt; and &gt;, inclusive<BR>
# ( or, between &lt; and EOL or line beginning and &gt; )<BR>
#<BR>
&nbsp;&nbsp;&nbsp;foreach ( keys %input ) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$input{$_} =~ s/&lt;([^&gt;]|\n)*&gt;//g;
<BR>
&nbsp;&nbsp;&nbsp;}<BR>
}<BR>
<BR>
sub print_header {<BR>
<BR>
&nbsp;&nbsp;&nbsp;print &lt;&lt;END;<BR>
&lt;HTML&gt;<BR>
&lt;HEAD&gt;&lt;TITLE&gt;Richard's First Example Chat Room&lt;/TITLE&gt;&lt;/HEAD&gt;
<BR>
&lt;BODY&gt;<BR>
&lt;CENTER&gt;<BR>
&lt;P&gt;&lt;FONT SIZE=+2&gt;&lt;B&gt;<BR>
My First Chat Room<BR>
&lt;/B&gt;&lt;/FONT&gt;<BR>
&lt;BR&gt;&lt;BR&gt;<BR>
&lt;FONT SIZE=-1&gt;&lt;B&gt;&lt;I&gt;<BR>
This chat room is the beginning example of the CGI Programming
Unleashed<BR>
Chat Room Example.&nbsp;&nbsp;While fully functional, I've left
some &quot;hooks&quot; for<BR>
later improvement.<BR>
&lt;/I&gt;&lt;/B&gt;&lt;/FONT&gt;<BR>
&lt;/CENTER&gt;<BR>
&lt;BR&gt;&lt;HR&gt;<BR>
END<BR>
<BR>
}<BR>
<BR>
sub print_form {<BR>
<BR>
#<BR>
# if this is the first access of a chatting session, create the
ID#<BR>
# and put a message into the handle string<BR>
#<BR>
&nbsp;&nbsp;&nbsp;if ( !(defined($input{'id'})) ) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$input{'handle'} = 'Put your
handle here!';<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;($input{'id'} = $ip) =~ s/\.//g;
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;substr($input{'id'},$[,3)
= ''; # removes first 3 digits of IP info<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for $i ( 0 .. (length($browsername)-1)
) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$input{'id'}
+= ord(substr($browsername,$[+$i,1));<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} # adds Browser info to end
of the string<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$input{'id'} .= &quot;.$$&quot;;
# process ID becomes part of ID#<BR>
&nbsp;&nbsp;&nbsp;}<BR>
<BR>
&nbsp;&nbsp;&nbsp;print &lt;&lt;END;<BR>
&lt;FORM METHOD=POST ACTION=$baseurl/$progname&gt;<BR>
&lt;PRE&gt;<BR>
&lt;B&gt;Name&nbsp;&nbsp;&nbsp;&nbsp; :&lt;/B&gt; &lt;INPUT TYPE=&quot;text&quot;
SIZE=40 NAME=&quot;handle&quot; MAXLENGTH=&quot;40&quot; &Acirc;VALUE=&quot;$input{'handle'}&quot;&gt;
<BR>
&lt;INPUT TYPE=&quot;hidden&quot; NAME=&quot;id&quot; VALUE=&quot;$input{'id'}&quot;&gt;
<BR>
&lt;A NAME=&quot;Comments&quot;&gt;&lt;B&gt;Comments :&lt;/B&gt;
<BR>
&lt;TEXTAREA NAME=&quot;comments&quot; ROWS=3 COLS=50&gt;&lt;/TEXTAREA&gt;&lt;/A&gt;
<BR>
&lt;/PRE&gt;<BR>
&lt;BR&gt;<BR>
&lt;INPUT TYPE=&quot;submit&quot; VALUE=&quot;Submit Comments
or Update Room&quot;&gt;<BR>
&lt;INPUT TYPE=&quot;reset&quot; VALUE=&quot;Clear Form&quot;&gt;
<BR>
&lt;/FORM&gt;<BR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -