动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   PHP → 阅读文章

 模拟OICQ的实现思路和核心程序

作者来源: 
阅读 数 184 人次 , 2006-3-29 4:08:00 


根据许多网友需求,特地把我站的这个模拟 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 发言内容

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>";
}
?>

其中的 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=javascript>window.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"); // 判断用户是否合法在线的公用程序
?>
<html>
<head>
<title>短信息</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<style type="text/css">
td {font-size:9pt}
</style>
<script 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.0
window.open(theurl,winname,features);
}
//-->
</script>
</head>

<body 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("
","\r\n",$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">
<tr>
<td colspan="2" height="20" bgcolor="99cc99" align="center">查看短信息</td>
</tr>
<tr>
<td colspan="2" height="20"><?echo date("m月d日 h:i",$tmp['date'])." ".$sendernickname[0]?> 给你[<?echo $name?>]留言:</td>
</tr>
<form name=viewtalk action=shortalk.php method=post>
<input type=hidden name=talkto value=<?echo $tmp['sender']?>>
<input type=hidden name=action value=send>
<input 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>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="toreply" value="回复留言 enter">
<a href="#" onclick="mm_openbrwindow('memberviewtalk.php?talkto=<?echo $tmp['sender']?>','viewtalk','scrollbars=yes')">聊天纪录</a> </td>
</tr>
</form>
</table>
<script language="javascript">
document.viewtalk.toreply.focus();
</script>
<?
}
else if ($action == "sendbegin")
{
$replymessage = ereg_replace("\r\n","
",$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=sendbegin>
<tr align="center">
<td colspan="2" height="20" bgcolor="99cc99">发送短信息</td>
</tr>
<tr 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']."</option>\n";
else
echo "<option value='".$msg['name']."'>".$msg['nickname']."</option>\n";
}
?>
</select>
请短于500字符 </td>
</tr>
<tr align="center">
<td colspan="2" height="146" valign="top">
<textarea name="replymessage" cols="40" rows="9"></textarea>
</td>
</tr>
<tr 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="#" onclick="mm_openbrwindow('memberviewtalk.php?talkto=<?echo $talkto?>','viewtalk','scrollbars=yes')">聊天纪录</a></td>
</tr>
</form>
</table>
<script language="javascript">
document.sendmsg.replymessage.focus();
</script>
<?
}
?>
</body>
</html>

 本文TagsQQ  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:PHP拥有序列化方法
· 下一篇:写的一个比较烂的目录文件列表程序,支持多系统,可按时间排序,可进入多层目录
· 关于SESSION的补充
· 访问属性和方法 -- Classes and Objects in PHP5
· PHP中常见的session问题
· VML绘图板④简化的服务器端--server.php、server.asp
· PHPShop存在多个安全漏洞


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077