快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

PHP-Push技术实现刷新功能

  Server push 前一段时间炒得很热的“推”技术,不过网上大部分都是cgi的资料,偶然看到一个法国的网站上有这么个介绍,可惜法语看不懂,只能从他的程序中看懂点东西,现整理个例子出来大家学习一下。可以用于聊天室的数据传输、网站上的新闻更新、等等各类更新频繁的页面。

以前做刷新主要通过页面上加标签。

< META HTTP-EQUIV=REFRESH CONTENT="time;URL=url" >

或者使用javascript的timeout+reload,不过这种刷新的方式取决于时间的设定,无法连续的数据传输且时间不好确定。采用了Server push的服务器在客户机做出一个哀求后,和客户机建立一个永久的连接,然后服务器会根据客户机的哀求不断地把数据包推向服务器。那些你觉察不到的延迟会让你觉得服务器的响应和你的哀求已经达到了同步的程度。

先来看一下例子再解释。

 img.php

  < ?php
    set_time_limit(0);
    $file = "./1.jpg";
    $sep = "gIrLsKiCkAsSiTsAySsOoNaTsHiRt";
  if(ereg(".*MSIE.*",$HTTP_SERVER_VARS["HTTP_USER_AGENT"])){
  //假如是ie浏览器,直接输出就退出,IE的不支持哦,我没试出来过
    header("Cache-Control: no-cache");
    header("Pragma: no-cache");
    header("Content-type: image/jpeg");
    header("Content-size: " . filesize($file));
    readfile($file);
  }else{
    header("Content-Type: multipart/x-mixed-replace; boundary=$sep");
  //这里是要害哦,看看MIME类型说明

  //你会明白
  print "--$sep
";
  do{
    print "Content-Type: image/jpeg
";
    readfile($file);
    print "
--$sep
";
    flush();
    $mt = filemtime($file);
    do{
      sleep (1);
      clearstatcache();
     }while($mt == filemtime($file));
  }while(1);
}
? >

这就是一个永久执行的页面(网络不断的情况下),不断输出图片的内容,下面是调用的页面。<img src=img.php>,然后打开你的netscape或其他非ie浏览器查看调用页面,好象没什么变化啊,别急,接着就是怎样变动1.jpg这个图片了,写个另外的php页面来测试吧,比如弄2张图片按时间来覆盖1.jpg(这个方式自己想,用拷贝覆盖也行,只要1.jpg有变化)。这时你就看到调用页面的图片自动更新了。

使用中你会发现个问题:怎么图片不自动更新了。这是由于客户机在一段时间内没有对服务器发生哀求,也就是某一段时间内没有新的内容向浏览器输入,可能发生连接超时现象。什么办法解决呢?可以在执行页面中加个向浏览器发送一个空信号,类似ftp连接方法,上面页面中在do...while(1)间加个print("");

以上是转的部分,由于比较有爱好,在GOOGLE上找了一下,大家可以看看下面的资料.

Requirements
Works with Apache-1.3.14/PHP4.0.3pl1 server and Various Netscape clients. Probably many other server combos. Tested on Netscape 4.7x and 6.0/Mozilla.
Does NOT WORK WITH IE. Internet Exploiter does not support x-mixed-replace server-push as far as I know. If a browser has "MSIE" in its User-Agent string the script will display one image and exit.

Update 20020108: Poked around freshmeat for a bit and found Andy Wilcock\\'s Cambozola java applet which seems to work well with my php script to make the stream viewable under IE. Beware that the current version doesn\\'t work under "Name-based" virtual hosts but I\\'ll have a patch for it soon.
Source
Download

<?
$file = "./latest.jpg";
$sep = "gIrLsKiCkAsSiTsAySsOoNaTsHiRt";

if (ereg(".*MSIE.*",$HTTP_SERVER_VARS["HTTP_USER_AGENT"]))
{
# If IE, spit out one pic and exit
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Content-type: image/jpeg");
header("Content-size: " . filesize($file));
readfile($file);
}
else
{
# if not IE, give the browser a try
header("Content-Type: multipart/x-mixed-replace; boundary=$sep");
print "--$sepn";
do {
print "Content-Type: image/jpegnn";
readfile($file);
print "n--$sepn";
flush();
$mt = filemtime($file);
do {
sleep (1);
# we won\\'t output the same image twice.
clearstatcache();
} while ($mt == filemtime($file));
} while (1);
}
?>

Make sure there are no blank lines outside the <? ?> in your script. That will cause screwey headers to be sent too soon.
Reference the script in your HTML page as if it was an image:

<IMG SRC="server-push.php" HEIGHT=240 WIDTH=320>

Use this bit of PHP on the page that references the image to compensate for IE\\'s lack of "innovation":

<HEAD>
<?
if (ereg("MSIE",$HTTP_SERVER_VARS["HTTP_USER_AGENT"])) {
echo "<META HTTP-EQUIV="Refresh" CONTENT="4;">n";
}
?>
</HEAD>
返回类别: 教程
上一教程: 利用PHP动态生成VRML网页
下一教程: 在PHP3中实现SESSION的功能(二)

您可以阅读与"PHP-Push技术实现刷新功能"相关的教程:
· PHP6将实现的几个特性/功能
· PHP实现的功能是显示8条基色色带
· 在PHP3中实现SESSION的功能(三、示例程序:test_session.php3)
· 在PHP3中实现SESSION的功能(二)
· 在PHP3中实现SESSION的功能(一、SESSION函数库:session.inc.php3)(转译)
    微笑服务 优质保证 索取样品