Shanghai WTO Forum

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 3346|回复: 0

基于PHP的聊天室编程思想

[复制链接]
发表于 2007-12-3 17:54:31 | 显示全部楼层 |阅读模式
<><STRONG>聊天室编程思想-- 大门--登陆</STRONG><BR>大门--登陆</P><>1 页面登陆的基本要素<BR>你可以在我的竹叶看到登陆 的表单,这里提供了最基本的登陆表单项<BR>(1)登陆表单<BR><form method=POST name=chatform action=chat/login.php?action=enter onSubmit="b1_submit();return true;" target="howtodo"><BR>(a)聊天表单的名字为chatform,我使用action=enter作为进入聊天室的入口,如果没有这个参数,则显示登陆页 面.<BR>(b)在表单提交时,先调用b1_submit()建立聊天的窗口<BR>┝奶斓哪勘甏翱谖猙1_submit()建立 的howtodo窗口</P><>(2)表单项<BR>昵称:<input type=text name=name size=15 maxlength="10"><BR>密码:<input type=password name=pass size=15 maxlength="10"><BR><input type=submit name=submit value=登陆 style="width:100"><BR><input type=reset name=reset value=重添 style="width:50"><BR>(a)各表单项一定要设定最大允许长度 maxlength</P><>(3)建立聊天窗口的js<BR><script LANGUAGE="javascript"><BR>function b1_submit(){<BR>chat=window.open('',"howtodo",'Status=no,scrollbars=no,resizable=no');<BR>chat.moveTo(0,0);<BR>chat.resizeTo(screen.availWidth,screen.availHeight);<BR>chat.outerWidth=screen.availWidth;<BR>chat.outerHeight=screen.availHeight;<BR>}<BR>这段代码先 打开一个没有状态栏,滚动条,可调整尺寸的howtodo窗口!然后移动到屏幕左上角,然后放大到允许的屏幕大小. </P><><STRONG>聊天室编程思想--大门 -- 通行证</STRONG></P><>大门 -- 通行证<BR>聊天室可以采用完全自由的方式运行,你可以随意 输入呢称,不用密码,不保存你的聊天状态,优点是:自由,非常适合于游客!另外一个方法是注册聊天室,每个进入 聊天室的人都要输入自己的用户名和密码才能进入!优点:充分体现个性,非常适合于老朋友,他们的呢称不会被 人恶意侵占使用.我的聊天室使用注册方法!</P><>注册通常采用2种方法:1,先注册然后进入聊天;2,自动注 册,然后在里面修改自己的资料!我采用第2种方法!!每个新进入的聊友的用户名会被自动保存到注册到数据库内 ,下次登陆必须输入准确的密码才能进入!</P><>下面是判断部分!本程序使用文本数据库 !</P><>//$useronline为在线人的数据文件名称<BR>//$useronlinelock为在线人的锁定标志 <BR>//$register为已经注册的数据文件名称<BR>//$registerlock为注册文件的锁定标志<BR>//$split为分隔 符</P><>//登陆参数 enter<BR>if($action == "enter")<BR>{<BR>//当前时间秒数<BR>$timecurrent = date("U");</P><P>//锁定在线人数文件,防止同时修改同一个文件<BR>while( file_exists($useronlinelock))<BR>{<BR>if(!file_exists($useronlinelock))<BR>{<BR>break;<BR>}<BR>}</P><P>//创建临时文件 <BR>fclose(fopen($useronlinelock,"w"));</P><P>//读入在线用户和已经注册用户的信息:密码,昵称,更新时间 <BR>$useronline = file($useronline);<BR>$register = file($register);</P><P>//用于判断登 陆是否成功的标志<BR>$namesign=0;</P><P>//判断用户名,密码的错误,用户名不允许为空,不允许超过10 个字符,密码不允许超过20个字符<BR>if(($name =="") || (strlen($name) > 10) || (strlen($pass) > 20) )<BR>{<BR>print("没有昵称或密码过长");<BR>//登陆失败<BR>$namesign=1;<BR>//删除临时文件<BR>unlink($useronlinelock);<BR>} <BR>else<BR>{<BR>//查找是否已经有人注册或者密码错误<BR>$foundsign=0;<BR>for($i=0;$i<count($register);$i++)<BR>{<BR>//分割<BR>$tempregister = split($split,$register[$i],99);<BR>//找到已经注册的用户名<BR>if( $name == $tempregister[0] )<BR>{<BR>//已经找到标志<BR>$foundsign=1;<BR>//密码正确吗<BR>if($pass != $tempregister[1])<BR>print("密码错了!");<BR>//登陆失败<BR>$namesign=1;<BR>unlink($useronlinelock);<BR>break;<BR>}<BR>else<BR>{<BR>//老用户登陆成功<BR>$namesign=0;<BR>break;<BR>}<BR>}</P><P>}</P><P>//如果没有找到这个用户名,那么就自动注册 <BR>if(!$foundsign)<BR>{<BR>//保存用户名和密码<BR>$handle = fopen($register,"a");<BR>fputs($handle,"$name$split$pass$split ");<BR>fclose($handle);<BR>//新 用户登陆成功<BR>$namesign=0;<BR>}<BR>}<BR>}<BR>if(!$namesign)<BR>{ <BR>//更新在线人的名单<BR>$useronlinehandle = fopen($useronline,"w");</P><P>//判断是否已经在里面,只是刷新页面<BR>$updatesign = 0;<BR>for($i=0;$i<count($useronline);$i++)<BR>{<BR>$usertemp=split($split,chop($useronline[$i]),99);<BR>if($name == $usertemp[0])<BR>{<BR>//更新标志<BR>$updatesign = 1;<BR>fputs($useronlinehandle,$useronline[$i]);<BR>}<BR>else<BR>{<BR>fputs($useronlinehandle,$useronline[$i]);<BR>}<BR>}<BR>//如 果没有在里面,则增加到里面<BR>if(!$updatesign)<BR>fputs($useronlinehandle,"$name$split$level$split$pass$split$timecurrent ");<BR>fclose($useronlinehandle);</P><P>//去掉缩定<BR>unlink($useronlinelock);</P><P>//登陆成 功<BR>}</P><P>到这里,用户的验证已经完成,聊友已经合法的进入了聊天室,携带者呢称和密码 </P><P><BR><STRONG>聊天室编程思想--大厅 -- 显示界面</STRONG></P><P><BR>大厅 -- 显示界面<BR>2000年09月04<BR>现在的www聊天室基本全部采用框架方式,可以用 frame也可以用iframe看个人喜欢了,我的采用frame的传统方式</P><P>print("<frameset rows="*,110,0,0,0" border=0> ");<BR>print("<frameset cols="660,118" rows="*"> ");</P><P>//主显示屏幕,负责显示聊天内容<BR>print("<frame name=u src=about:blank frameborder="NO" noresize> ");</P><P>//在线人数屏幕<BR>print("<frame name=r src="about:blank" frameborder="NO">");<BR>print("</frameset> ");</P><P>//发送信息的屏幕,信息指挥中心,所有指令都要由这里发出<BR>print("<frame name=d src=send.php?name=$name&&pass=$pass scrolling='no' frameborder="NO" noresize> ");</P><P>//被动更新屏幕,处理发送的信息<BR>print("<frame src="about:blank" name="bl"> ");</P><P>/主动更新屏幕,显示自己和其他聊友的聊天信息<BR>print("<frame src="about:blank" name="flush"> ");</P><P>//检测是否在线的屏幕,对于异常 离开,如死机,掉线等的处理<BR>print("<frame src="about:blank" name="check"> ");<BR>print("</frameset> ");</P><P>因为各个页面之间的程序有 联系,所以显示顺序很重要,可以看到,我这里只有发送页面不是about:blank,其他页面的显示都要先通过发送页 面的调用才能开始.</P><P><BR><STRONG>聊天室编程思想--大厅 -- 在线人数</STRONG></P><P>大厅 -- 在线人数<BR><BR>我根据网易聊天室的在线人数的方法,显示当前的在 线人数,代码解释如下:</P><P>1 登陆时建立在线人名单的数组,放在body后面</P><P><?<BR>//锁定在线 人数文件<BR>while(file_exists($useronlinelock)){$pppp++;}<BR>fclose(fopen($useronlinelock,"w"));</P><P>//读入在线人名单<BR>$useronline = file($useronline);<BR>unlink($useronlinelock);</P><P>//建立数组 list<BR>print("document.writeln("list=new Array(");<BR>$k=count($useronline);<BR>if($k>1)<BR>{<BR>for($i=0;$i<($k-1);$i++)<BR>{<BR>$usercurrent = split($split,$useronline[$i],99);<BR>// 姓名+,<BR>print("'$usercurrent[0]',");<BR>}<BR>$i=$k-1;<BR>// 处理最后一个姓名 <BR>$usercurrent = split($split,$useronline[$i],99);<BR>print("'$usercurrent[0]'");<BR>}<BR>// 数组结束<BR>print(")"); ");<BR>?></P><P>2显示在 线人数的js<BR>document.writeln('[在线人数<font color=red>'+count+'</font>]<br>');<BR>document.writeln("[<a href="javascript:parent.cs('所有人')">所有人</a>]<br>");<BR>document.writeln("<font class='p9'>");<BR>var j,name,club;<BR>for(var i=0;i<list.length;i=i+1)<BR>{<BR>if(list!=null){</P><P>//显示每个在线人的名字 <BR>document.writeln("<a href="javascript:parent.cs('"+list+"')" title='"+list+"'>"+list+"</a><br>");<BR>}<BR>}<BR>this.r.document.writeln('</font><hr>');</P><P><BR>3改变聊天对象<BR>function cs(name)<BR>{<BR>if(this.d.document==null)return;<BR>if(name=='所有人')<BR>{<BR>this.d.add('所有人');<BR>this.d.document.inputform.talkto.value='所有人 ';</P><P>//改变焦点<BR>this.d.document.inputform.msg.focus();<BR>return;<BR>}<BR>for(var i=0;i<list.length;i=i+1)<BR>{<BR>if(list==name)<BR>{</P><P>//更改发送的谈话对象<BR>this.d.document.inputform.talkto.value=list;<BR>this.d.document.inputform.msg.focus();<BR>return;<BR>}<BR>}</P><P>//错误<BR>alert('此用户已离线或已改了昵称。');<BR>}</P><P>4删除一个用户 <BR>function del(str)<BR>{<BR>for(var i=0;i<list.length;i=i+1)<BR>if(list==str)<BR>{<BR>delete list;<BR>count--;<BR>}<BR>}</P><P><BR>5增加一个用户 <BR>function add(str1,str2)<BR>{<BR>var l=list.length;<BR>for(var i=0;i<list.length;i=i+1)</P><P>//如果已经在数组里面则返回<BR>if(list==str1)<BR>return;</P><P>//增加一个用户<BR>list[l]=str1;<BR>count++;<BR>}</P><P>6更新聊天人数的方法,定时器的使用<BR>var timerID=null;<BR>var timerRunning=false;</P><P>function stop()<BR>{<BR>//停止<BR>if(timerRunning)clearTimeout(timerID);<BR>timerRunning=false;<BR>}<BR>function start()<BR>{<BR>stop();<BR>//调用更新在线人数的程序<BR>write1();<BR>}</P><P>function write1()<BR>{<BR>... ... ... ...<BR>//设定更新时间,<BR>timerID=setTimeout("start()",30000);<BR>timerRunning=true;<BR>}</P><P><BR>这种方法比较简单的实现了在线人数的显示,当然也可以使用读入在线 人文件的方法显示在线人数,不过在改变聊天对象是会比较麻烦.</P><P><BR><STRONG>聊天室编程思想--指挥中心 -- 发送信息</STRONG></P><P>指挥中心 -- 发送信息<BR>这里是聊天室的指挥中心,所有的指令都要在这里发出</P><P>1下面是基本的发送表单代码</P><P><form name=inputform action='messagesend.php' target='bl' onsubmit='return(checksay());' method=POST></P><P><?<BR>//下面的2个参数用于验证信息的正确性<BR>print("<input type='hidden' name='name' value='$name'> ");<BR>print("<input type='hidden' name='pass' value='$pass'> ");<BR>?> </P><P>//聊天对象,注意加上 readonly 属性<BR><input type="text" name="talkto" size="10" maxlength="20" readonly value="所有人"></P><P>//上次聊天的发送内容<BR><input type='hidden' name='message' value=''></P><P>//发送的表单文本框<BR><input type="text" name="msg" maxlength="120" size="34"></P><P><input type="submit" name="Submit" value="发送"></P><P></form></P><P>2 检查发送内容的js</P><P>var dx ='';<BR>function checksay( )<BR>{</P><P>//不允许发送空的发言<BR>if(document.inputform.msg.value=='')<BR>{<BR>document.inputform.msg.focus();<BR>return false;<BR>}</P><P>//不允许重复发言,内容相同,对象相同<BR>if ((document.inputform.msg.value==document.inputform.message.value)&&(document.inputform.talkto.value==dx))<BR>{<BR>alert('发言不能重复');<BR>document.inputform.msg.focus();<BR>return false;<BR>}</P><P>//两次发言内容的间隔不能小于1秒,或者发言字数大于间隔*3<BR>t2=(new Date()).getTime()/1000;<BR>if(((t2-t1)<1)||((t2-t1)*3<document.inputform.msg.value.length))<BR>{<BR>document.inputform.msg.focus();<BR>return false;<BR>}</P><P>//更新时间<BR>t1=t2;</P><P>document.inputform.showsign.value=1;</P><P>//保存上次发言内容<BR>document.inputform.message.value =document.inputform.msg.value;</P><P>//清空发言内容<BR>document.inputform.msg.value ='';</P><P>//保存发言对象<BR>dx=document.inputform.talkto.value;</P><P>//定位焦点<BR>document.inputform.msg.focus();</P><P>//返回<BR>return(true);<BR>}</P><P>3调用信息发送程序,发布聊天者已经进入的信息<BR><script><BR>parent.bl.document.open();<BR>parent.bl.document.write("<meta http-equiv='refresh' content='0;url=messagesend.php?name=<? print($name); ?>&&action=enter&&pass=<? print($pass); ?>'>")<BR>parent.bl.document.close();<BR></script></P><P>发言由messagesend.php处理完成,注意输出对象为bl,也就是处理发言的框架名称,这样保证发言框架的页面内容的完整</P><P><BR><STRONG>聊天室编程思想--主动更新与被动更新</STRONG></P><P><BR>主动更新与被动更新<BR><BR>聊天的内容如何显示在屏幕上,一种是每隔一段时间刷新一次页面,读入全部聊天内容,然后显示,这里采用的是js的document.write的方法实现不刷新的聊天页面!</P><P>1 主页面的生成,规定了CSS类型,显示欢迎词<BR>function write2(){<BR>if(this.u.document==null)return;<BR>this.u.document.writeln("<html><head>");<BR>this.u.document.writeln("<meta http-equiv=Content-Type content=text/html; charset=gb2312>");<BR>this.u.document.writeln("<style type=text/css>");<BR>this.u.document.writeln(".p9 { font-size: 11pt; line-height: 15pt}");<BR>this.u.document.writeln("body { font-size: 11pt; line-height: 15pt}");<BR>this.u.document.writeln("a:visited { font-size: 11pt;color: #0000FF; text-decoration: none;}");<BR>this.u.document.writeln("a:link { font-size: 11pt;color: #0000FF; text-decoration: none}");<BR>this.u.document.writeln("a:hover { font-size: 11pt;color: #FF0000}");<BR>this.u.document.writeln("</style>");</P><P>this.u.document.writeln("</head>");<BR>this.u.document.writeln("<body);<BR>//.................. 这里插入生成在线人数组的程序段</P><P><BR>this.u.document.writeln("<script>");<BR>this.u.document.writeln("<p class=p9 align=left>");<BR>this.u.document.writeln("<p align=center>欢迎光临PlayBoy聊天室,本聊天室正在测试阶段,如有问题请与<a href=mailto:pccastle@sina.com>我们联系</a></p>");<BR>}</P><P>2 初始化进入信息,第一次进入聊天室</P><P>if($action == "enter")<BR>{</P><P>/////////////////// 调用显示主屏幕的js程序 ////////////////////<BR>print("parent.write2(); ");</P><P>//发言内容,某某进入聊天室了<BR>$message = "<a href=javascript:parent.cs('$name'); target=d>$name</a>来到聊天室".$message." ".date("m月d日 H:i")."<script>parent.add('$name','$photo');parent.write1();</script><br>";<BR>}<BR>//更新发言内容<BR>while(file_exists($lockfile)){ $pppp++; }</P><P>//发言的锁定<BR>fclose(fopen($lockfile,"w"));</P><P>//读入发言的总句数,也就是所有人一共发了多少言!我们可以保存每一个发言,但是这样会占用大量的磁盘空间,我们采用了一种取模的方法,循环使用文件来减少文件操作!<BR>$talkmessage = file($filename);<BR>$number = chop($talkmessage[0]);</P><P>//发言数增加一,然后保存<BR>$talkhandle = fopen($filename,"w");<BR>$number++;<BR>fputs($talkhandle,$number);<BR>fclose($talkhandle);</P><P>/去掉锁定<BR>unlink($lockfile);</P><P>//对发言总数对10取模,作为文件名保存发言内容,也就是说第11句和第1句使用同一个文件名,由于不可能同时有10句话没有更新,所以这是数在人不是非常多的情况下很好!当然,考虑到人多的情况,可以设成100.<BR>$filehandle = fopen("messageonline".($number%10).".php","w");<BR>fputs($filehandle,$message);<BR>fclose($filehandle);</P><P>//显示进入信息<BR>print("parent.u.document.writeln("$message"); ");</P><P>//调用主动刷新js程序,传递已经显示的发言数目<BR>print("parent.flushwin($number) ");</P><P>//保存最后一次显示的发言<BR>$last = $number;<BR>}</P><P><BR>3 处理发送表单的请求</P><P>//不处理空的发言和超过一定数目的发言<BR>if( ($message != "")&&(strlen($message)<150))<BR>{</P><P>//检查发言者是否在线,防止意外<BR>$onlineperson = file("useronline.dbf");<BR>$personsign=0;<BR>for($i=0;$i<count($onlineperson);$i++)<BR>{<BR>$person = split($split,$onlineperson[$i],99);<BR>if($person[0] == $name)<BR>{<BR>$personsign = 1;<BR>$person[3] = date("U");<BR>break;<BR>}<BR>}</P><P>//在线时的处理程序<BR>if($personsign == 1)<BR>{</P><P>//添加发言时间的部分<BR>$message = $message." <font size=1>".date("m月d日 H:i")."</font><br>";</P><P>//锁定发言总数文件<BR>while(file_exists($lockfile)){ $pppp++; }<BR>fclose(fopen($lockfile,"w"));</P><P>//读入发言总数<BR>$talkmessage = file($filename);<BR>$number = chop($talkmessage[0]);</P><P>//总数加1,然后保存<BR>$talkhandle = fopen($filename,"w");<BR>$number++;<BR>fputs($talkhandle,$number);<BR>fclose($talkhandle);<BR>unlink($lockfile);</P><P>//总数对10取模后以文件形式保存发言内容<BR>$filehandle = fopen("messageonline".($number%10).".php","w");<BR>fputs($filehandle,$message);<BR>fclose($filehandle);<BR>}<BR>}</P><P>//////////////////////////////////////////////////////////////////<BR>这样,表单的处理已经完成,下面的主动更新程序将会把新的发言内容显示在屏幕上<BR>//////////////////////////////////////////////////////////////////</P><P>4 主动更新的自动循环调用方法</P><P>可以使用<meta http-equiv="reflesh" content="3;url=messageflush.php?name=<?print($name)?>&&pass=<?print($pass)&&last=<?print($last)?>的方式更新!</P><P>我的程序以前就是使用这种方法自动更新的,但是我发现一个问题,那就是当这个更新程序出现运行错误时,他不会产生调用下次更新的代码,造成后台更新程序停止工作!所以我采用了js定时的方法来完成同样的功能!</P><P>var flushtimeID=null;<BR>var flushRunning=false;</P><P>//上次更新标志<BR>var flushflag = true;</P><P>function flushstop()<BR>{<BR>if(flushtimerRunning)clearTimeout(flushtimerID);<BR>flushtimerRunning=false;<BR>}<BR>function flushstart()<BR>{<BR>flushstop();</P><P>//使用发送表单里面的上次显示的值<BR>flushwin(this.d.document.inputform.last.value);<BR>}</P><P>function flushwin(winnumber)<BR>{<BR>//如果上次更新正确,则调用下次更新<BR>if(flushflag == true)<BR>{<BR>url="messageflush.php?name=<? print($name); ?>&&pass=<? print($pass); ?>&&last="+winnumber;<BR>flush.location=url<BR>flushflag=false<BR>}</P><P>//否则等待一个循环<BR>flushtimerID=setTimeout("flushstart()",2000);<BR>flushtimerRunning=true;<BR>}</P><P>这种方法保证了在主程序不死的情况下,后台更新程序会一直运行下去!</P><P><BR>5 主动更新程序<BR><script Language='JavaScript'><BR><? <BR>//读入最大的发言数目<BR>$message = file($filename);<BR>$number = chop($message[0]);</P><P>//从上次显示的下一个发言开始到最大发言结束,显示发言内容<BR>for($i=$last+1;$i<=$number;$i++)<BR>{<BR>//读入下一个发言内容<BR>$filename = "messageonline".($i%10).".php";<BR>$message = file($filename);<BR>$tempmessage = split($split,$message[0],99);</P><P>//显示发言内容<BR>print("parent.u.document.writeln("$message[0]"); ");<BR>}</P><P>//更新发送表单最后一个发言的数目<BR>print("parent.d.document.inputform.last.value=$number; ");</P><P>//通知主程序本次更新已经完成<BR>print("parent.flushflag=true; ");<BR>?><BR></script></P><P><BR>这样,每个发送的发言,经过被动更新程序处理保存到文件内,然后由一个循环的主动更新程序完成显示任<BR>务!!! </P><P></P>
您需要登录后才可以回帖 登录 | 注册

本版积分规则


QQ|Archiver|mobile|The little black house|Shanghai WTO Net ( 沪ICP备10034107号-3 )

GMT+8, 2024-4-28 02:42

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表