php 获取输出内容长度,PHP 返回输出缓冲区内容的长度

用户评论:

Nitrogen (2009-02-14 09:34:19)

I use this function to produce a small speed test in my pages since I run my website from my home computers.

ob_start();$StartTime=microtime(1);?>.. webpage data goes here ..

Note: don't forget the "speed" depends on how quick the content is actually generated and not on the speed the data is sent to the client..

However it would be good to see such function to get the real speed. :)

Nitrogen.

stangelanda at arrowquick dot com (2007-08-03 08:36:23)

If you don't buffer your output, it doesn't seem like it would be possible for a web server to output a Content-Length header. For static files it can check the filesize, but for dynamic files that send output a little by little there is no way to know how many bytes it is going to output. And the headers have to be sent before a single byte is output.

Also if you're wondering why the Content-Length is important, the browser doesn't close the connection as long as the script is running. So if you register time consuming shutdown functions, the browser will still 'spin' waiting for more content until they complete. (I'm not sure this happens under all conditions, but I am certain the sending the Content-Length always prevents this.)

You might take note that this manual page does not currently output any Content-Length header.

Alternatively using mb_strlen($output, 'latin1') seems to work to determine bytes in a string. strlen may or may not depending on if it has been set up as a multibyte or not.

Sinured (2007-07-11 03:25:16)

> Dude, your web server will compute Content-length for you!

I've noticed that Apache 1.3 doesn't do that.

However, if you’re running Apache 2.2 (and maybe 2.0, haven't experienced that yet) you don't need to worry about Content-Length.

(2007-01-18 13:49:29)

Dude, your web server will compute Content-length for you!

rush at logic dot cz (2005-12-02 02:02:19)

There is a work-around for the situation you need to get length of the gz-ed buffer.

ob_start();

ob_start('ob_gzhandler');

... output the page content...

ob_end_flush(); // The ob_gzhandler one

header('Content-Length: '.ob_get_length());

ob_end_flush(); // The main one

more info at http://www.edginet.org/techie/website/http.html

prophp at gmail dot com (2005-10-14 23:56:23)

It's interesting how ob_start("ob_gzhandler"); or ob_gzhandler(); affect this function. The returned size is the buffer uncompressed size; therefore something like strlen(ob_get_contents()); just (hopefully) faster. However in order to obtain the compressed buffer size; there seem to be no function around.

Calin

webmaster at crescentart dot com (2001-06-29 13:58:42)

Here is an easy way to get the header Content-Lenght.

ob_start();

?>

Put HTML tags.

$size=ob_get_length();

header("Content-Length: $size");

ob_end_flush();

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值