Home Backend Development PHP Tutorial PHP中对缓冲区的统制实现代码

PHP中对缓冲区的统制实现代码

Jun 13, 2016 am 11:38 AM
info void

PHP中对缓冲区的控制实现代码

大家在使用PHP的过程中不免要使用到header和setcookie两个函数,这两个函数会发送一段文件头信息给浏览器,但是如果在使用这两个函数之前已经有了任何输出(包括空输出,比如空格,回车和换行)就会提示出错,提示信息如下:“Header had all ready send by”!那有什么方法可以在有了输出的情况下面在发送文件头信息呢?在PHP 4.0里面加入了缓冲区控制的几个函数,使用这些函数可以帮我们解决很多问题。

一、 相关函数简介:

1、Flush:输出缓冲区内的内容并且删除缓冲区。

函数格式:flush()

说明:这个函数经常使用,效率很高。

2、ob_start :打开输出缓冲区

函数格式:void ob_start(void)

说明:当缓冲区激活时,所有来自PHP程序的非文件头信息均不会发送,而是保存在内部缓冲区。为了输出缓冲区的内容,可以使用ob_end_flush()或者使用ob_end_clean()来输出缓冲区的内容。

3 、ob_get_contents :返回内部缓冲区的内容。

使用方法:string ob_get_contents(void)

说明:这个函数会返回当前缓冲区中的内容,如果输出缓冲区没有激活,则返回 FALSE 。

4、ob_get_length:返回内部缓冲区的长度。

使用方法:int ob_get_length(void)

说明:这个函数会返回当前缓冲区中的长度;和ob_get_contents一样,如果输出缓冲区没有激活。则返回 FALSE。

5、ob_end_flush :发送内部缓冲区的内容到浏览器,并且关闭输出缓冲区。

使用方法:void ob_end_flush(void)

说明:这个函数发送输出缓冲区的内容(如果有的话)。

6、ob_end_clean:删除内部缓冲区的内容,并且关闭内部缓冲区

使用方法:void ob_end_clean(void)

说明:这个函数不会输出内部缓冲区的内容!

7、ob_implicit_flush:打开或关闭绝对刷新

使用方法:void ob_implicit_flush ([int flag])

说明:使用过Perl的人都知道?$|=x的意义,这个字符串可以打开/关闭缓冲区,而ob_implicit_flush函数也和那个一样,默认为关闭缓冲区,打开绝对输出。

二、使用例子:

在一开始,笔者说了用缓冲区控制的函数可以防止文件头发送信息出错,下面就是一个例子:

?

.代码如下:

//PHP提示符
ob_start(); //打开缓冲区
echo "Hello/n"; //输出
header('location:gotourl.php'); //把浏览器重定向到gotourl.php
?>

?

如果去掉ob_start,PHP就会提示在文件的第4行出错(出错信息如前面所示),但是加上ob_start,就不会提示出错,原因是当打开了缓冲区,echo后面的字符不会输出到浏览器,而是保留在服务器,直到你使用flush或者ob_end_flush才会输出,所以并不会有任何文件头输出的错误!

下面再给出一个很经典的用途:

比如你用得到服务器和客户端的设置信息,但是这个信息会因为客户端的不同而不同,如果想要保存phpinfo()函数的输出怎么办呢?在没有缓冲区控制之前,可以说一点办法也没有,但是有了缓冲区的控制,我们可以轻松的解决:

?

.代码如下:


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_clean();
??????? //设置文件头;?
??????? Header("Content-type: image/PNG");

??????? //以PNG格式将图像输出到浏览器或文件;
??????? ImagePNG($distortion_im);

??????? //销毁一图像,释放与image关联的内存;
??????? ImageDestroy($distortion_im);
??????? ImageDestroy($im);

?

来源:http://www.poluoluo.com/jzxy/201309/243650.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does void mean in java What does void mean in java Mar 01, 2023 pm 07:19 PM

In Java, void means "empty", that is, "returns nothing". When the method is declared, it means that the method has no return value. ​void corresponds to a wrapper class "java.lang.Void". The Void class is modified with final and is a non-instantiable placeholder class used to save a reference to the Class object that represents the Java keyword void.

What is the significance of using void type return value in PHP? What is the significance of using void type return value in PHP? Apr 10, 2024 pm 09:21 PM

In PHP, the void type return value means that the function does not return any value, and is usually used for operations such as updating records that do not require a return value. Use the void keyword when declaring a void function; when calling a void function, the result must not be assigned to a variable. Practical case: void type return value can be used to update database records without returning any information.

The function of void keyword in C language The function of void keyword in C language Feb 19, 2024 pm 11:33 PM

void in C is a special keyword used to represent an empty type, which means data without a specific type. In C language, void is usually used in the following three aspects. The function return type is void. In C language, functions can have different return types, such as int, float, char, etc. However, if the function does not return any value, the return type can be set to void. This means that after the function is executed, it does not return a specific value. For example: voidhelloWorld()

How to understand and solve 'javascript:void(O)' problems How to understand and solve 'javascript:void(O)' problems Feb 19, 2024 pm 05:35 PM

What does javascript:void(0) mean? What are the solutions to this problem? When we browse the web, sometimes we encounter some links that do not respond after clicking, but are displayed as "javascript:void(0)" in the browser's address bar. This question may confuse some web visitors as they don't know what the error message, which literally looks like JavaScript code, means. So, let's unpack this together

How does a C++ function return void type? How does a C++ function return void type? Apr 20, 2024 pm 01:06 PM

The void function in C++ does not return any value, and its syntax is voidfunction_name(). Common uses include: Entering user input, such as getting the user's age and printing it to the console.

How to solve javascriptvoid(o) How to solve javascriptvoid(o) Nov 23, 2023 am 11:02 AM

Solutions to javascriptvoid(o): 1. Check for syntax errors; 2. Ensure the correct execution environment; 3. Check for conflicts with other codes; 4. Use event delegation; 5. Use other binding methods; 6. Check external resources; 7. Use debugging tools; 8. Update dependent libraries or plug-ins; 9. Check character encoding; 10. Refactor the code to reduce dependencies. Solving javascriptvoid(0) errors requires careful inspection of the code, execution environment, and related dependencies.

The role of void in C language The role of void in C language Apr 03, 2025 pm 04:12 PM

In C language, void is a keyword that indicates no return value. It is used in various scenarios, such as: a function that declares no return value: void print_message(); a function that declares no parameter: void print_message(void); a function that defines no return value: void print_message() { printf("Hello world\n"); } A function that defines no parameter: void print_message(void) { printf("Hell

How to turn off info information output in thinkphp5 How to turn off info information output in thinkphp5 Jun 03, 2023 am 11:49 AM

1. The function of info Before we start to close info, we need to understand its function. In the ThinkPHP5 framework, there are three main forms of info information output: displaying the currently accessed URL and request parameters; displaying debugging information such as the SQL statement execution and running time of the current page at the bottom of the page; outputting detailed error information when an execution error occurs, which is convenient Although debugging seems useful, most of the info information is not very helpful to real developers. Often, what we need is some more concise output so we can better focus on development. 2. Close info. Closing info is very simple. You only need to configure

See all articles