基于php的聊天室编程思想


大门--登陆
2000年09月04
1 页面登陆的基本要素
你可以在我的竹叶看到登陆 的表单,这里提供了最基本的登陆表单项
(1)登陆表单
<form method=POST name=chatform action=chat/login.php?action=enter onSubmit="b1_submit();return true;" target="howtodo">
(a)聊天表单的名字为chatform,我使用action=enter作为进入聊天室的入口,如果没有这个参数,则显示登陆页 面.
(b)在表单提交时,先调用b1_submit()建立聊天的窗口
(c)聊天的目标窗口为b1_submit()建立 的howtodo窗口

(2)表单项
昵称:<input type=text name=name size=15 maxlength="10">
密码:<input type=password name=pass size=15 maxlength="10">
<input type=submit name=submit value=登陆 style="width:100">
<input type=reset name=reset value=重添 style="width:50">
(a)各表单项一定要设定最大允许长度 maxlength

(3)建立聊天窗口的js
<script LANGUAGE="javascript">
function b1_submit(){
chat=window.open('',"howtodo",'Status=no,scrollbars=no,resizable=no');
chat.moveTo(0,0);
chat.resizeTo(screen.availWidth,screen.availHeight);
chat.outerWidth=screen.availWidth;
chat.outerHeight=screen.availHeight;
}
这段代码先 打开一个没有状态栏,滚动条,可调整尺寸的howtodo窗口!然后移动到屏幕左上角,然后放大到允许的屏幕大小.

 

 聊天室编程思想--大门 -- 通行证

发表于:中国PHP联盟 www.phpx.com 作者/来源:php2000/php2000.com 人气:2087


大门 -- 通行证
2000年09月04
聊天室可以采用完全自由的方式运行,你可以随意 输入呢称,不用密码,不保存你的聊天状态,优点是:自由,非常适合于游客!另外一个方法是注册聊天室,每个进入 聊天室的人都要输入自己的用户名和密码才能进入!优点:充分体现个性,非常适合于老朋友,他们的呢称不会被 人恶意侵占使用.我的聊天室使用注册方法!

注册通常采用2种方法:1,先注册然后进入聊天;2,自动注 册,然后在里面修改自己的资料!我采用第2种方法!!每个新进入的聊友的用户名会被自动保存到注册到数据库内 ,下次登陆必须输入准确的密码才能进入!

下面是判断部分!本程序使用文本数据库 !

file://$useronline为在线人的数据文件名称
file://$useronlinelock为在线人的锁定标志
file://$register为已经注册的数据文件名称
file://$registerlock为注册文件的锁定标志
file://$split为分隔 符

file://登陆参数 enter
if($action == "enter")
{
file://当前时间秒数
$timecurrent = date("U");

file://锁定在线人数文件,防止同时修改同一个文件
while( file_exists($useronlinelock))
{
if(!file_exists($useronlinelock))
{
break;
}
}

file://创建临时文件
fclose(fopen($useronlinelock,"w"));

file://读入在线用户和已经注册用户的信息:密码,昵称,更新时间
$useronline = file($useronline);
$register = file($register);

file://用于判断登 陆是否成功的标志
$namesign=0;

file://判断用户名,密码的错误,用户名不允许为空,不允许超过10 个字符,密码不允许超过20个字符
if(($name =="") || (strlen($name) > 10) || (strlen($pass) > 20) )
{
print("没有昵称或密码过长");
file://登陆失败
$namesign=1;
file://删除临时文件
unlink($useronlinelock);
}
else
{
file://查找是否已经有人注册或者密码错误
$foundsign=0;
for($i=0;$i<count($register);$i++)
{
file://分割
$tempregister = split($split,$register[$i],99);
file://找到已经注册的用户名
if( $name == $tempregister[0] )
{
file://已经找到标志
$foundsign=1;
file://密码正确吗
if($pass != $tempregister[1])
print("密码错了!");
file://登陆失败
$namesign=1;
unlink($useronlinelock);
break;
}
else
{
file://老用户登陆成功
$namesign=0;
break;
}
}

}

file://如果没有找到这个用户名,那么就自动注册
if(!$foundsign)
{
file://保存用户名和密码
$handle = fopen($register,"a");
fputs($handle,"$name$split$pass$split ");
fclose($handle);
file://新 用户登陆成功
$namesign=0;
}
}
}
if(!$namesign)
{
file://更新在线人的名单
$useronlinehandle = fopen($useronline,"w");

file://判断是否已经在里面,只是刷新页面
$updatesign = 0;
for($i=0;$i<count($useronline);$i++)
{
$usertemp=split($split,chop($useronline[$i]),99);
if($name == $usertemp[0])
{
file://更新标志
$updatesign = 1;
fputs($useronlinehandle,$useronline[$i]);
}
else
{
fputs($useronlinehandle,$useronline[$i]);
}
}
file://如 果没有在里面,则增加到里面
if(!$updatesign)
fputs($useronlinehandle,"$name$split$level$split$pass$split$timecurrent ");
fclose($useronlinehandle);

file://去掉缩定
unlink($useronlinelock);

file://登陆成 功
}

到这里,用户的验证已经完成,聊友已经合法的进入了聊天室,携带者呢称和密码

 


聊天室编程思想--大厅 -- 显示界面

发表于:中国PHP联盟 www.phpx.com 作者/来源:php2000/php2000.com 人气:1617


大厅 -- 显示界面
2000年09月04
现在的www聊天室基本全部采用框架方式,可以用 frame也可以用iframe看个人喜欢了,我的采用frame的传统方式

print("<frameset rows="*,110,0,0,0" border=0> ");
print("<frameset cols="660,118" rows="*"> ");

file://主显示屏幕,负责显示聊天内容
print("<frame name=u src=about:blank frameborder="NO" noresize> ");

file://在线人数屏幕
print("<frame name=r src="about:blank" frameborder="NO">");
print("</frameset> ");

file://发送信息的屏幕,信息指挥中心,所有指令都要由这里发出
print("<frame name=d src=send.php?name=$name&&pass=$pass scrolling='no' frameborder="NO" noresize> ");

file://被动更新屏幕,处理发送的信息
print("<frame src="about:blank" name="bl"> ");

/主动更新屏幕,显示自己和其他聊友的聊天信息
print("<frame src="about:blank" name="flush"> ");

file://检测是否在线的屏幕,对于异常 离开,如死机,掉线等的处理
print("<frame src="about:blank" name="check"> ");
print("</frameset> ");

因为各个页面之间的程序有 联系,所以显示顺序很重要,可以看到,我这里只有发送页面不是about:blank,其他页面的显示都要先通过发送页 面的调用才能开始.


聊天室编程思想--大厅 -- 在线人数

发表于:中国PHP联盟 www.phpx.com 作者/来源:php2000/php2000.com 人气:1936


大厅 -- 在线人数
2000年09月04
我根据网易聊天室的在线人数的方法,显示当前的在 线人数,代码解释如下:

1 登陆时建立在线人名单的数组,放在body后面

<?
file://锁定在线 人数文件
while(file_exists($useronlinelock)){$pppp++;}
fclose(fopen($useronlinelock,"w"));
 
file://读入在线人名单
$useronline = file($useronline);
unlink($useronlinelock);
 
file://建立数组 list
print("document.writeln("list=new Array(");
$k=count($useronline);
if($k>1)
{
for($i=0;$i<($k-1);$i++)
{
$usercurrent = split($split,$useronline[$i],99);
// 姓名+,
print("'$usercurrent[0]',");
}
$i=$k-1;
// 处理最后一个姓名 
$usercurrent = split($split,$useronline[$i],99);
print("'$usercurrent[0]'");
}
// 数组结束
print(")"); ");
?>
2显示在 线人数的js
document.writeln('[在线人数<font color=red>'+count+'</font>]<br>');
document.writeln("[<a href="javascript:parent.cs('所有人')">所有人</a>]<br>");
document.writeln("<font class='p9'>");
var j,name,club;
for(var i=0;i<list.length;i=i+1)
{
if(list[i]!=null){

file://显示每个在线人的名字
document.writeln("<a href="javascript:parent.cs('"+list[i]+"')" title='"+list[i]+"'>"+list[i]+"</a><br>");
}
}
this.r.document.writeln('</font><hr>');


3改变聊天对象
function cs(name)
{
if(this.d.document==null)return;
if(name=='所有人')
{
this.d.add('所有人');
this.d.document.inputform.talkto.value='所有人 ';

file://改变焦点
this.d.document.inputform.msg.focus();
return;
}
for(var i=0;i<list.length;i=i+1)
{
if(list[i]==name)
{

file://更改发送的谈话对象
this.d.document.inputform.talkto.value=list[i];
this.d.document.inputform.msg.focus();
return;
}
}

file://错误
alert('此用户已离线或已改了昵称。');
}

4删除一个用户
function del(str)
{
for(var i=0;i<list.length;i=i+1)
if(list[i]==str)
{
delete list[i];
count--;
}
}


5增加一个用户
function add(str1,str2)
{
var l=list.length;
for(var i=0;i<list.length;i=i+1)

file://如果已经在数组里面则返回
if(list[i]==str1)
return;

file://增加一个用户
list[l]=str1;
count++;
}

6更新聊天人数的方法,定时器的使用
var timerID=null;
var timerRunning=false;

function stop()
{
file://停止
if(timerRunning)clearTimeout(timerID);
timerRunning=false;
}
function start()
{
stop();
file://调用更新在线人数的程序
write1();
}

function write1()
{
... ... ... ...
file://设定更新时间,
timerID=setTimeout("start()",30000);
timerRunning=true;
}


这种方法比较简单的实现了在线人数的显示,当然也可以使用读入在线 人文件的方法显示在线人数,不过在改变聊天对象是会比较麻烦.


聊天室编程思想--指挥中心 -- 发送信息

发表于:中国PHP联盟 www.phpx.com 作者/来源:php2000/php2000.com 人气:1504


指挥中心 -- 发送信息
2000年09月04
这里是聊天室的指挥中心,所有的指令都要在这里发出

1下面是基本的发送表单代码

<form name=inputform action='messagesend.php' target='bl' onsubmit='return(checksay());' method=POST>

<?
file://下面的2个参数用于验证信息的正确性
print("<input type='hidden' name='name' value='$name'> ");
print("<input type='hidden' name='pass' value='$pass'> ");
?>

 

file://聊天对象,注意加上 readonly 属性
<input type="text" name="talkto" size="10" maxlength="20" readonly value="所有人">

file://上次聊天的发送内容
<input type='hidden' name='message' value=''>

file://发送的表单文本框
<input type="text" name="msg" maxlength="120" size="34">

<input type="submit" name="Submit" value="发送">

</form>

2 检查发送内容的js

var dx ='';
function checksay( )
{

file://不允许发送空的发言
if(document.inputform.msg.value=='')
{
document.inputform.msg.focus();
return false;
}

file://不允许重复发言,内容相同,对象相同
if ((document.inputform.msg.value==document.inputform.message.value)&&(document.inputform.talkto.value==dx))
{
alert('发言不能重复');
document.inputform.msg.focus();
return false;
}

file://两次发言内容的间隔不能小于1秒,或者发言字数大于间隔*3
t2=(new Date()).getTime()/1000;
if(((t2-t1)<1)||((t2-t1)*3<document.inputform.msg.value.length))
{
document.inputform.msg.focus();
return false;
}

file://更新时间
t1=t2;

document.inputform.showsign.value=1;

file://保存上次发言内容
document.inputform.message.value =document.inputform.msg.value;

file://清空发言内容
document.inputform.msg.value ='';

file://保存发言对象
dx=document.inputform.talkto.value;

file://定位焦点
document.inputform.msg.focus();

file://返回
return(true);
}

3调用信息发送程序,发布聊天者已经进入的信息
<script>
parent.bl.document.open();
parent.bl.document.write("<meta http-equiv='refresh' content='0;url=messagesend.php?name=<? print($name); ?> &&action=enter&&pass= <? print($pass); ?> '>")
parent.bl.document.close();
</script>

发言由messagesend.php处理完成,注意输出对象为bl,也就是处理发言的框架名称,这样保证发言框架的页面内容的完整


聊天室编程思想--主动更新与被动更新

发表于:中国PHP联盟 www.phpx.com 作者/来源:php2000/php2000.com 人气:1803


主动更新与被动更新
2000年09月04
聊天的内容如何显示在屏幕上,一种是每隔一段时间刷新一次页面,读入全部聊天内容,然后显示,这里采用的是js的document.write的方法实现不刷新的聊天页面!

1 主页面的生成,规定了CSS类型,显示欢迎词
function write2(){
if(this.u.document==null)return;
this.u.document.writeln("<html><head>");
this.u.document.writeln("<meta http-equiv=Content-Type content=text/html; charset=gb2312>");
this.u.document.writeln("<style type=text/css>");
this.u.document.writeln(".p9 { font-size: 11pt; line-height: 15pt}");
this.u.document.writeln("body { font-size: 11pt; line-height: 15pt}");
this.u.document.writeln("a:visited { font-size: 11pt;color: #0000FF; text-decoration: none;}");
this.u.document.writeln("a:link { font-size: 11pt;color: #0000FF; text-decoration: none}");
this.u.document.writeln("a:hover { font-size: 11pt;color: #FF0000}");
this.u.document.writeln("</style>");

this.u.document.writeln("</head>");
this.u.document.writeln("<body);
//.................. 这里插入生成在线人数组的程序段


this.u.document.writeln("<script>");
this.u.document.writeln("<p class=p9 align=left>");
this.u.document.writeln("<p align=center>欢迎光临PlayBoy聊天室,本聊天室正在测试阶段,如有问题请与<a href=mailto:pccastle@sina.com>我们联系</a></p>");
}

2 初始化进入信息,第一次进入聊天室

if($action == "enter")
{

/////////////////// 调用显示主屏幕的js程序 ////////////////////
print("parent.write2(); ");

file://发言内容,某某进入聊天室了
$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>";
}
file://更新发言内容
while(file_exists($lockfile)){ $pppp++; }

file://发言的锁定
fclose(fopen($lockfile,"w"));

file://读入发言的总句数,也就是所有人一共发了多少言!我们可以保存每一个发言,但是这样会占用大量的磁盘空间,我们采用了一种取模的方法,循环使用文件来减少文件操作!
$talkmessage = file($filename);
$number = chop($talkmessage[0]);

file://发言数增加一,然后保存
$talkhandle = fopen($filename,"w");
$number++;
fputs($talkhandle,$number);
fclose($talkhandle);

/去掉锁定
unlink($lockfile);

file://对发言总数对10取模,作为文件名保存发言内容,也就是说第11句和第1句使用同一个文件名,由于不可能同时有10句话没有更新,所以这是数在人不是非常多的情况下很好!当然,考虑到人多的情况,可以设成100.
$filehandle = fopen("messageonline".($number%10).".php","w");
fputs($filehandle,$message);
fclose($filehandle);

file://显示进入信息
print("parent.u.document.writeln("$message"); ");

file://调用主动刷新js程序,传递已经显示的发言数目
print("parent.flushwin($number) ");

file://保存最后一次显示的发言
$last = $number;
}


3 处理发送表单的请求

file://不处理空的发言和超过一定数目的发言
if( ($message != "")&&(strlen($message)<150))
{

file://检查发言者是否在线,防止意外
$onlineperson = file("useronline.dbf");
$personsign=0;
for($i=0;$i<count($onlineperson);$i++)
{
$person = split($split,$onlineperson[$i],99);
if($person[0] == $name)
{
$personsign = 1;
$person[3] = date("U");
break;
}
}

file://在线时的处理程序
if($personsign == 1)
{

file://添加发言时间的部分
$message = $message." <font size=1>".date("m月d日 H:i")."</font><br>";

file://锁定发言总数文件
while(file_exists($lockfile)){ $pppp++; }
fclose(fopen($lockfile,"w"));

file://读入发言总数
$talkmessage = file($filename);
$number = chop($talkmessage[0]);

file://总数加1,然后保存
$talkhandle = fopen($filename,"w");
$number++;
fputs($talkhandle,$number);
fclose($talkhandle);
unlink($lockfile);

file://总数对10取模后以文件形式保存发言内容
$filehandle = fopen("messageonline".($number%10).".php","w");
fputs($filehandle,$message);
fclose($filehandle);
}
}

//////////////////////////////////////////////////////////////////
这样,表单的处理已经完成,下面的主动更新程序将会把新的发言内容显示在屏幕上
//////////////////////////////////////////////////////////////////

4 主动更新的自动循环调用方法

可以使用<meta http-equiv="reflesh" content="3;url=messageflush.php?name= <?
print($name)?>&&pass=<?
print($pass)&&last=<?
print($last)?>的方式更新!

我的程序以前就是使用这种方法自动更新的,但是我发现一个问题,那就是当这个更新程序出现运行错误时,他不会产生调用下次更新的代码,造成后台更新程序停止工作!所以我采用了js定时的方法来完成同样的功能!

var flushtimeID=null;
var flushRunning=false;

file://上次更新标志
var flushflag = true;

function flushstop()
{
if(flushtimerRunning)clearTimeout(flushtimerID);
flushtimerRunning=false;
}
function flushstart()
{
flushstop();

file://使用发送表单里面的上次显示的值
flushwin(this.d.document.inputform.last.value);
}

function flushwin(winnumber)
{
file://如果上次更新正确,则调用下次更新
if(flushflag == true)
{
url="messageflush.php?name=<? print($name); ?> &&pass= <? print($pass); ?> &&last="+winnumber;
flush.location=url
flushflag=false
}

file://否则等待一个循环
flushtimerID=setTimeout("flushstart()",2000);
flushtimerRunning=true;
}

这种方法保证了在主程序不死的情况下,后台更新程序会一直运行下去!


5 主动更新程序
<script Language='JavaScript'>

<? 
file://读入最大的发言数目
$message = file($filename);
$number = chop($message[0]);
 
file://从上次显示的下一个发言开始到最大发言结束,显示发言内容
for($i=$last+1;$i<=$number;$i++)
{
file://读入下一个发言内容
$filename = "messageonline".($i%10).".php";
$message = file($filename);
$tempmessage = split($split,$message[0],99);
 
file://显示发言内容
print("parent.u.document.writeln("$message[0]"); ");
}
 
file://更新发送表单最后一个发言的数目
print("parent.d.document.inputform.last.value=$number; ");
 
file://通知主程序本次更新已经完成
print("parent.flushflag=true; ");
?>
</script>

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3