|
用php控制用户的浏览器--ob*函数的使用 |
|
output control 函数可以让你自由控制脚本中数据的输出。它非常地有用,特别是对于:当你想在数据已经输出后,再输出文件头的情况。输出控制函数不对使用 header() 或 setcookie(), 发送的文件头信息产生影响,只对那些类似于 echo() 和 php 代码的数据块有作用。 我们先举一个简单的例子,让大家对output control有一个大致的印象: <?php
ob_start(); //打开缓冲区 echo "hellon"; //输出 header("location:index.php"); //把浏览器重定向到index.php ob_end_flush();//输出全部内容到浏览器 ?>
一、 相关函数简介 二、深入了解 1. 关于flush函数: <?php
for($i = 1; $i <= 300; $i++) print(" "); // 这一句话非常关键,cache的结构使得它的内容只有达到一定的大小才能从浏览器里输出 // 换言之,如果cache的内容不达到一定的大小,它是不会在程序执行完毕前输出的。经 // 过测试,我发现这个大小的底限是256个字符长。这意味着cache以后接收的内容都会 // 源源不断的被发送出去。 for($j = 1; $j <= 20; $j++) { echo $j . " "; flush(); //这一部会使cache新增的内容被挤出去,显示到浏览器上 sleep(1); //让程序"睡"一秒钟,会让你把效果看得更清楚 } ?> 具体效果你可以到这里看看http://www.php2000.com/~uchinaboy/out.php
<?php
ob_start(); //打开缓冲区 phpinfo(); //使用phpinfo函数 $info = ob_get_contents(); //得到缓冲区的内容并且赋值给$info $file = fopen('info.txt', 'w'); //打开文件info.txt fwrite($file, $info); //写入信息到info.txt fclose($file); //关闭文件info.txt ?> 用以上的方法,就可以把不同用户的phpinfo信息保存下来,这在以前恐怕没有办法办到!其实上面就是将一些“过程”转化为“函数”的方法! 可能现在大家对ob_start()的功能有了一定的了解,上面的一个例子看似简单,但实际上已经掌握了使用ob_start()的要点。 来,让我们看看能用ob系列函数做些什么……
简介:所谓静态模版技术就是通过某种方式,使得用户在client端得到的是由php产生的html页面。如果这个html页面不会再被更新,那么当另外的用户再次浏览此页面时,程序将不会再调用php以及相关的数据库,对于某些信息量比较大的网站,例如sina,163,sohu。类似这种的技术带来的好处是非常巨大的。 我所知道的实现静态输出的有两种办法: <?php
ob_start();//打开缓冲区 ?>
<?php
$content = ob_get_contents(); //取得php页面输出的全部内容 $fp = fopen("output00001 . html", "w"); //创建一个文件,并打开,准备写入 fwrite($fp, $content); //把php页面的内容全部写入output00001.html,然后…… fclose($fp); ?>
(二)、 捕捉输出 以上的example 4.是一种最简单的情况,你还可以在写入前对$content进行操作…… <?php
function run_code($code) { if ($code) { ob_start(); eval($code); $contents = ob_get_contents(); ob_end_clean(); } else { echo "错误!没有输出"; exit(); } return $contents; } ?> 以上这个例子的用途不是很大,不过很典型$code的本身就是一个含有变量的输出页面,而这个例子用eval把$code中的变量替换,然后对输出结果再进行输出捕捉,再一次的进行处理…… example 6. 加快传输 <?php
/* ** title.........: php4 http compression speeds up the web ** version.......: 1.20 ** author........: catoc <catoc@163.net> ** filename......: gzdoc.php ** last changed..: 18/10/2000 ** requirments...: php4 >= 4.0.1 ** php was configured with --with-zlib[=dir] ** notes.........: dynamic content acceleration compresses ** the data transmission data on the fly ** code by sun jin hu (catoc) <catoc@163.net> ** most newer browsers since 1998/1999 have ** been equipped to support the http 1.1 ** standard known as "content-encoding." ** essentially the browser indicates to the ** server that it can accept "content encoding" ** and if the server is capable it will then ** compress the data and transmit it. the ** browser decompresses it and then renders ** the page. ** ** modified by john lim (jlim@natsoft.com.my) ** based on ideas by sandy mcarthur, jr ** usage........: ** no space before the beginning of the first '<?' tag. ** ------------start of file---------- ** |<? ** | include('gzdoc.php'); ** |? > ** |<html> ** |... the page ... ** |</html> ** |<? ** | gzdocout(); ** |? > ** -------------end of file----------- */ ob_start(); ob_implicit_flush(0); function checkcangzip() { global $http_accept_encoding; if (headers_sent() || connection_timeout() || connection_aborted()) { return 0; } if (strpos($http_accept_encoding, 'x-gzip') !== false) return "x-gzip"; if (strpos($http_accept_encoding, 'gzip') !== false) return "gzip"; return 0; } /* $level = compression level 0-9, 0=none, 9=max */ function gzdocout($level = 1, $debug = 0) { $encoding = checkcangzip(); if ($encoding) { print "n<!-- use compress $encoding -->n"; $contents = ob_get_contents(); ob_end_clean(); if ($debug) { $s = "<p>not compress length: " . strlen($contents); $s .= "compressed length: " . strlen(gzcompress($contents, $level)); $contents .= $s; } header("content-encoding: $encoding"); print "x1fx8bx08x00x00x00x00x00"; $size = strlen($contents); $crc = crc32($contents); $contents = gzcompress($contents, $level); $contents = substr($contents, 0, strlen($contents) - 4); print $contents; print pack('v', $crc); print pack('v', $size); exit; } else { ob_end_flush(); exit; } } ?> 这是catoc的一段很早以前的代码,是在weblogs.com看到的,他利用了zlib的函数,对传输的内容进行了压缩,测试表明,对于10k以上的页面,会产生效果,而且页面越大,效果越明显…… |
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!