手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Php编程>列表

最小化数据传输——在客户端存储数据

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

"loader"文件:
====================
<?php

session_start(); // 在这使用 Sessions !

if(!isset($timestamp)) {
//如果"timestamp"没有定义,则定义并设为0
$timestamp=0;
}

$dab=mysql_connect("localhost","user","password"); // 打开数据库
mysql_select_db("testbase",$dab);

// 查找客户端所没有的信息
$query="select * from testeable where timestamp>'$timestamp'";
$result=mysql_query($query,$dab);
$msgs=array();

// 在这个循环,我们存储最新消息/数据,并设置"timestamp"为当前最大值

while($res=mysql_fetch_array($result)) {
$msgs[]=$res["message"];
if($res["timestamp"]>$timestamp) {
$timestamp=$res["timestamp"];
}
}
session_register("timestamp"); // 注册"timestamp"变量

echo '<script>';

// 在这个循环我们生成javascript代码
// 把最新从服务端得到的数据存储到"master"页面里(注意:使用"top"指向最顶窗口(master)

for($i=0;$i<$count($msgs);$i ) {
?>
top.lines[top.lines.length]="<?php print("$msgs[$i]"); ?>";
<?php
}

//现在我们将生成"javascript"代码 ,使 "display"页刷新

?>
top.display.location.reload();
</script>

<!-- 注意是用 javascript 的"setInterval()" 方法使得"loader"页面每隔4秒刷新一次 -->
<body onLoad="window.setInterval('location.reload()',4000);">
</body>
=======================================

"form"页面:
====================
<?php

session_start();

if (!isset($timestamp)) {
$timestamp=0;
}

// 显示表单,产生"timestamp"变量.
if (isset($msg)) {
$dab=mysql_connect("localhost","root","seldon");
mysql_select_db("testbase",$dab);
$query="insert into testeable(timestamp,message) values(now(),'$msg')";
mysql_query($query,$dab);
// 得到timestamp 后的所有消息
$query="select * from testeable where timestamp>'$tt'";
$result=mysql_query($query,$dab);
$msgs=array();$i=0;$timestamp=0;
while($res=mysql_fetch_array($result)) {
$msgs[]=$res["message"];
if($res["timestamp"]>$timestamp) {
$tt=$res["timestamp"];
}
}
session_register("timestamp");

?>
<script>
<?php
for($i=0;$i<$count($msgs);$i ) {
?>
top.lines[top.lines.length]="<?print("$msgs[$i]");?>";
<?php
}
?>
top.display.location.reload(); //刷新"display"页
</script>
<?php
}
?>
<form name="foo" action="<?php print("$PHP_SELF"); ?>" method="post">
Message:<input type="text" name="msg">
<input type="submit" name="newmsg" value="send">
</form>
=====================================================
注:我们使得在"form"页提交发言时,马上刷新"display"页面,这可以达到对发言人来说马上发言马上
显示,更体现实时性。

正如您所看到的,这个聊天室很简单,这就是最小化客户/服务(C/S)数据传输技能,使用这个技术您可以达到最
小的数据传输,再次提醒您:该技术并不是这用于聊天室!
=======================================================
以上在win98 apache1.3 php4.03平台测试通过!
如有建议或疑问请和飞扬社区联系(http://feiyschool.51.net)
或者发EMAIL到feiyhy@sina.com
请您多多赐教,谢谢!
===============================
英文原作者:Luis Argerich, Alejandro Mitrou
英文原文所在网址:http://www.phpbuilder.com

【本文版权归作者与奥索网共同拥有,如需转载,请注明作者及出处】

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!