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