PHP应用实例集锦_聊天室
第1章 PHP概述
程序6-1
聊天室
程序6-2
//显示聊天公约内容
– 1 –
PHP编程基础及应用实例集锦
新用户注册
新用户注册
如果您希望加入本聊天室,必须遵守如下的《聊天公约》,正式注册后才可以进入本聊天室
聊天公约:
1. 请遵守《计算机信息网络国际联网安全保护
管理办法
关于高温津贴发放的管理办法稽核管理办法下载并购贷款管理办法下载商业信用卡管理办法下载处方管理办法word下载
》里的一切规定。
2. 尊重网上道德。
3. 承担一切因您的行为而直接或间接导致的民事或刑事法律责任。
4. 网管有权把违反规定者清理出去、情况恶劣的注销其名字。
return; endif;?>
//生成供用户输入注册信息的
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
单
请填写如下表格进行注册,名称和密码不能超过8个字符(标有**项为必填项):
|
|
程序6-3
//产生用户注销页面
聊天室
用户注销
//删除该用户
此用户已删除!";
}
else{
echo "输入有误,请重新输入!";
}
mysql_close();
}
?>
程序6-4
文件名:save.php
你的密码和确认密码不一致!";
elseif ($newname=="" || $newpass=="" || $birthday=="" ||
$newtishi=="" || $newdaan=="")
echo "您忘了哪项必填项没有填!";
elseif (strlen($newname)>8 || strlen($newpass)>8)
echo "名称或密码请不要超过8个字符!";
– 5 –
PHP编程基础及应用实例集锦
elseif (strchr($newname,"<") || strchr($newname,">"))
echo "名称中不能有大、小于号等特殊字符!";
else{//保存用户注册信息
……
$query=mysql_query("select * from renyuan where name='$newname'");
$array=mysql_fetch_array($query);
if ($array[name]==$newname) {
echo "此名已存在,请换个试试!";
}
else {
$newdaan=md5($newdaan);
$pass=md5($newpass);
$pass=substr($pass,0,32);
$newname=htmlspecialchars($newname);
$newemail=htmlspecialchars($newmail);
$newtishi=htmlspecialchars($newtishi);
$query=mysql_query("insert into man values('$newname','$pass','1','
$newemail','',now(),'$birthday','$newtishi','$newdaan')");
if($query) {
echo "注册成功请记好你的名称: $newname
密码: $newpass
你现在的点数为1点
超过30天不登录的用户将被系统自动删除~
关闭窗口";}
else{
echo "系统忙,
请等回再注册!";}
}
mysql_close();
}
?>
程序6-5:
[$roomname]聊天室欢迎您有(".$rows.")人在线
";
echo " 所有人
";
while($array=mysql_fetch_array($query)){
echo " $array[sex]
$array[name]
";
if ($fen>$array[time]){
$t = date(H时i分s秒);
}
echo "
";
?>
程序6-6
//生成动态下拉菜单
程序6-7
//读入表情动作文件
$emote3 = file($emotefilename); $emote3number = count($emote3); for($kk=0;$kk<$emote3number;$kk++)
{
//分割每个表情动作
$emote=split($split,chop($emote3[$kk]),99);
//判断发言内容是否等于表情动作
if($message == $emote[0])
{
//替换表情动作里面的对象为实际的聊天者的名字
$emote[1]=ereg_replace("对象","$talkto",$emote[1]);
//将发言内容改为动作表情
$message = "
$name".$emote[1];
break;
}
}
?>
程序6-8
php
/*设置一个记载交谈模式的变量$secret_talk ,通过判断该变量的当前值,确定当前的交谈是否为密
谈*/
if($secret_talk == "on")//是密谈
{
/*在发言内容前面附加如下格式的头标记:
其中$split代表分隔符,secret为私聊标记,后面的是聊天对象和自己的名字*/
$message = "
密谈$message"; }
– 8 –
第1章 PHP概述
else
{
//如果第一个标记为 open 表示公开聊天,后面同样为对象和自己的名字
$message = "$message";
}
//将交谈内容保存到文件中
……
?>
程序6-9
//解析发言内容
$tempmessage = split($split,$message[0],99); /*如果发言不是密谈,或者私聊对象是自己,再或者发送这句私聊的是自己,则显示这句话,否则
不显示*/
if (($tempmessage[1] != "secret") ||
($tempmessage[2] == $name) ||
($tempmessage[3] == $name) )
{
//显示密谈内容
print("parent.u.document.writeln("$message[0]"); "); }
?>
程序6-10
function write2(){
if(this.u.document==null)
return;
this.u.document.writeln("
");
this.u.document.writeln("");
this.u.document.writeln("");
this.u.document.writeln("");
this.u.document.writeln("");
this.u.document.writeln("");
this.u.document.writeln("
显示欢迎词
");
}
程序6-11
if($action == "enter")
{
print("parent.write2(); ");
//发言内容
$message = "$name
来到聊天室".$message." ".date("m月d日 H:i").
"
";
}
//更新发言内容
while(file_exists($lockfile)){
$pppp++;
}
//锁定保存发言内容的文件
fclose(fopen($lockfile,"w"));
//读入发言的总条数
$talkmessage = file($filename); $number = chop($talkmessage[0]); //发言数加1,然后保存
$talkhandle = fopen($filename,"w"); $number++;
fputs($talkhandle,$number);
fclose($talkhandle);
//解除文件的锁定
unlink($lockfile);
/*对发言总数对10取模,作为文件名保存发言内容,即第11句和第1句使用同一个文件名。这样
– 10 –
第1章 PHP概述
做的依据是任何聊天室都不可能同时有10句话而没有更新。为了保险,可以将此数设为更大的数*/ $filehandle = fopen("messageonline".($number%10).".php","w");
fputs($filehandle,$message);
fclose($filehandle);
//显示进入信息
print("parent.u.document.writeln("$message"); "); //调用主动刷新程序,并显示发言数目
print("parent.flushwin($number) "); //保存最后一次显示的发言
$last = $number;
}
程序6-12
//不处理空的发言和超过一定数目的发言
if( ($message != "")&&(strlen($message)<150)) {
//检查发言者是否在线
$onlineperson = file("useronline.dbf");
$personsign=0;
for($i=0;$i".date("m月d日 H:i")."
";
//锁定发言总数文件
while(file_exists($lockfile)){ $pppp++; }
fclose(fopen($lockfile,"w"));
//读入发言总数
– 11 –
PHP编程基础及应用实例集锦
$talkmessage = file($filename);
$number = chop($talkmessage[0]);
//总数加1,然后保存
$talkhandle = fopen($filename,"w");
$number++;
fputs($talkhandle,$number);
fclose($talkhandle);
unlink($lockfile);
//总数对10取模后以文件形式保存发言内容
$filehandle = fopen("messageonline".($number%10).".php","w");
fputs($filehandle,$message);
fclose($filehandle);
}
}
程序6-13
var flushtimeID=null;
var flushRunning=false;
//上次更新标志
var flushflag = true;
function flushstop()
{
if(flushtimerRunning)clearTimeout(flushtimerID);
flushtimerRunning=false;
}
function flushstart()
{
flushstop();
//使用发送表单里面的上次显示的值
flushwin(this.d.document.inputform.last.value);
}
function flushwin(winnumber)
{
//如果上次更新正确,则调用下次更新
if(flushflag == true)
{
url="messageflush.php?name= print($name); ?>&&
– 12 –
第1章 PHP概述
pass= print($pass); ?>&&last="+winnumber;
flush.location=url;
flushflag=false;
}
//否则等待一个循环
flushtimerID=setTimeout("flushstart()",2000);
flushtimerRunning=true;
}
程序6-14
程序6-15
2. 检查发送内容
程序6-16
var dx ='';
function checksay( )
{
//不允许发送空的发言
if(document.inputform.msg.value=='') {
document.inputform.msg.focus();
return false;
}
//不允许重复发言
if ((document.inputform.msg.value==document.inputform.message.value)&&
(document.inputform.talkto.value==dx)) {
alert('不能重复发言');
document.inputform.msg.focus();
return false;
}
//两次发言内容的间隔不能小于1秒,或者发言字数大于间隔*3
t2=(new Date()).getTime()/1000; if(((t2-t1)<1)||((t2-t1)*3
parent.bl.document.open();
parent.bl.document.write("")
parent.bl.document.close();
程序6-18
文件名:login.php
`
聊天室
程序6-19
文件名:input.php
聊天室
$name = str_replace ( "<", "<", $name);//用"<"代替$name中的所有的"<" $name = str_replace ( ">", ">", $name); $name = stripslashes (trim($name));//去掉字串中的反斜线字符
?>