根据许多网友需求,特地把我站的这个模拟 OICQ 的在线聊天的东西献给大家!
1 用户必须注册登陆,在数据库 userinfo 里面保存如下几个字段
Name 不用问了,这是登陆用的用户名,必须唯一
Password 登陆密码
NickName 用户昵称,也就是显示的名字
Face 存放着用户头像的编号,比如 01,代表 /images/face/01.gif 头像文件
OnlineStatus 用户是否在线的标志,在用户登陆的时候设置为 1
CurrentDate 用户最后访问/更新的时间,用于判断用户是否在线
聊天纪录 forumtalk 的结构为
CREATE TABLE forumtalk (id int(11) NOT NULL auto_increment,sender varchar(20) NOT NULL,receiver varchar(20) NOT NULL,date int(11) DEFAULT '0' NOT NULL,readsign tinyint(4) DEFAULT '0' NOT NULL,body varchar(200) NOT NULL,PRIMARY KEY (id),UNIQUE id_2 (id),KEY id (id));
其中 sender 是发送人的 Name
receiver 是接受人的 Name
date 是发言的时间
readsign 发言是否已经阅读过
body 发言内容
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/webkaifa/)2 显示在线用户的头像
?$onlineresult = mysql_query("select Name,NickName,Face,EnterTimes from userinfo where OnlineStatus=1 and CurrentDate ".(date("U")-120));$onlinenumber = mysql_num_rows($onlineresult);echo "欢迎光临,共有:".$onlinenumber."位朋友在线,按头像发短信息:";for($i=0;$i$onlinenumber;$i++){if(!$onlineuser = mysql_fetch_array($onlineresult))break;echo "a onClick=MM_openBrWindow('shortalk.php?talkto=".$onlineuser['Name']."','".$onlineuser['Name']."','width=300,height=250')img src='images/face/".$onlineuser['Face'].".gif' width=20 height=20 ";if($name == $onlineuser['Name'])echo "border=1 ";echo " title='代号:".$onlineuser['Name']."n昵称:".$onlineuser['NickName']."n来访:".$onlineuser['EnterTimes']."'/a";}?(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/webkaifa/)
其中的 onClick 用于弹出发送消息的对话窗口,大家可以在网页的源代码里面看到
3 在线用户的信息扫描和更新
在网页中使用内置框架来调用扫描和更新程序,这行也能在网页源代码里面看到!
iframe name=flush src="userflush.php" width="0" height="0" frameborder="0" scrolling="NO" marginwidth="0" marginheight="0" hspace="0" vspace="0"/iframe
4 信息扫描和更新程序 userflush.php
?session_start();mysql_connect("localhost","","");mysql_select_db("php2000");$delaytime=0;// 查找新的发言$query = "select * from forumtalk where readsign=0 and receiver='$name'";$result = mysql_query($query);if( mysql_num_rows($result) 0){// 读取和显示弹出窗口$msg = mysql_fetch_array($result);$numberfriend = $msg['id'];echo "script language=javascriptwindow.open('shortalk.php?action=view&talknumber=$numberfriend','_blank','width=300,height=250')/script";}// 设置当前用户的最新时间标志,表示它在线mysql_query("update userinfo set currentdate=".date("U")." where name='$name'");// 设置刷新时间间隔为15秒echo "meta http-equiv='refresh' content='15;url=userflush.php'";?
5 聊天信息的发送、阅读和回复程序 - shortalk.php
?require("require.php"); // 判断用户是否合法在线的公用程序?htmlheadtitle短信息/titlemeta http-equiv="Content-Type" content="text/html; charset=gb2312"style type="text/css"td {font-size:9pt}/stylescript language="Javascript"!--function docheck() {if (document.sendmsg.replymessage.value=="") {alert("缺少内容:需要输入您的留言内容");document.sendmsg.replymessage.focus();return(false);}return (true);}function MM_openBrWindow(theURL,winName,features) { //v2.0window.open(theURL,winName,features);}//--/script/headbody bgcolor="#DDDDFF"leftmargin="0" topmargin="0" background="phpchat_images/cloudtile.jpg"?if($action == "view"){$tmp = mysql_fetch_array(mysql_query("select sender,body,date from forumtalk where id=$talknumber and receiver='$name'"));$msg = $tmp['body'];$message = ereg_replace("","rn",$msg);mysql_query("update forumtalk set readsign=1 where id=$talknumber");$sendernickname = mysql_fetch_row(mysql_query("select nickname from userinfo where name='".$tmp['sender']."'"));?table width="300" border="0" cellspacing="0" cellpadding="0" height="200" bgcolor="f0f0f0"trtd colspan="2" height="20" bgcolor="99cc99" align="center"查看短信息/td/trtrtd colspan="2" height="20"?echo date("m月d日 h:i",$tmp['date'])." ".$sendernickname[0]? 给你[?echo $name?]留言:/td/trform name=viewtalk action=shortalk.php method=postinput type=hidden name=talkto value=?echo $tmp['sender']?input type=hidden name=action value=sendinput type=hidden name=talknumber value=?print($talknumber)?tr align="center"td colspan="2" height="146" valign="top"textarea name="textfield" cols="40" rows="11" readonly?print($message)?/textarea/td/trtrtd colspan="2" align="center"input type="submit" name="toreply" value="回复留言 Enter"a href="http://cms.ddvip.com/index.php#" onClick="MM_openBrWindow('memberviewtalk.php?talkto=?echo $tmp['sender']?','viewtalk','scrollbars=yes')"聊天纪录/a /td/tr/form/tablescript language="Javascript"document.viewtalk.toreply.focus();/script?}else if ($action == "sendbegin"){$replymessage = ereg_replace("rn","",$replymessage);$replymessage = ereg_replace("","",$replymessage);$replymessage = ereg_replace("","",$replymessage);$replymessage = substr($replymessage,0,2000);mysql_query("insert into forumtalk (sender,receiver,body,date) values ('$name','$talkto','$replymessage',".date("U").")");print("script language='javascript'window.close()/script");}else{?table width="300" border="0" cellspacing="0" cellpadding="0" height="200" bgcolor="f0f0f0"form name=sendmsg action=shortalk.php method=post OnSubmit=return(docheck());input type=hidden name=action value=sendbegintr align="center"td colspan="2" height="20" bgcolor="99cc99"发送短信息/td/trtr align="center"td colspan="2" height="20"发言对象:select name="talkto"?$result = mysql_query("select name,nickname from userinfo where onlinestatus=1");while($msg=mysql_fetch_array($result)){if($msg['name']==$talkto)echo "option value='".$msg['name']."' selected".$msg['nickname']."/optionn";elseecho "option value='".$msg['name']."'".$msg['nickname']."/optionn";}?/select请短于500字符 /td/trtr align="center"td colspan="2" height="146" valign="top"textarea name="replymessage" cols="40" rows="9"/textarea/td/trtr align="center"td colspan="4"?if($talknumber != ""){print("input type=button name=review value='查看前一留言 Alt+P' accesskey='p' onClick='javascript:history.go(-1)'");}?input type="submit" name="reply" value="开始新的发送留言 Alt+S" accesskey='s'a href="http://cms.ddvip.com/index.php#" onClick="MM_openBrWindow('memberviewtalk.php?talkto=?echo $talkto?','viewtalk','scrollbars=yes')"聊天纪录/a/td/tr/form/tablescript language="Javascript"document.sendmsg.replymessage.focus();/script?}?/body/html