output control 函数可以让你自由控制脚本中数据的输出。它非常地有用,特别是对于:当你想在数据已经输出后,再输出文件头的情况。输出控制函数不对使用 header() 或 setcookie(), 发送的文件头信息产生影响,只对那些类似于 echo() 和 php 代码的数据块有作用。
我们先举一个简单的例子,让大家对output control有一个大致的印象:
example 1.
php"><?php
ob_start(); //打开缓冲区
echo \"hellon\"; //输出
header(“location:index.php”); //把浏览器重定向到index.php
ob_end_flush();//输出全部内容到浏览器
?>
php"><?php
for($i = 1; $i <= 300; $i++ ) print(“ “);
// 这一句话非常关键,cache的结构使得它的内容只有达到一定的大小才能从浏览器里输出
// 换言之,如果cache的内容不达到一定的大小,它是不会在程序执行完毕前输出的。经
// 过测试,我发现这个大小的底限是256个字符长。这意味着cache以后接收的内容都会
// 源源不断的被发送出去。
for($j = 1; $j <= 20; $j++) {
echo $j.”
”;
flush(); //这一部会使cache新增的内容被挤出去,显示到浏览器上
sleep(1); //让程序“睡”一秒钟,会让你把效果看得更清楚
}
?>
php">比如你用得到服务器和客户端的设置信息,但是这个信息会因为客户端的不同而不同,如果想要保存phpinfo()函数的输出怎么办呢?在没有缓冲区控制之前,可以说一点办法也没有,但是有了缓冲区的控制,我们可以轻松的解决:
<?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
?>
php"><?php
ob_start();//打开缓冲区
?>
php页面的全部输出
<?
$content = ob_get_contents();//取得php页面输出的全部内容
$fp = fopen(“output00001.html”, “w”); //创建一个文件,并打开,准备写入
fwrite($fp, $content); //把php页面的内容全部写入output00001.html,然后……
fclose($fp);
?>
php"><?
function run_code($code) {
if($code) {
ob_start();
eval($code);
$contents = ob_get_contents();
ob_end_clean();
}else {
echo “错误!没有输出”;
exit();
}
return $contents;
}
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;
}
}
?>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器