First, let's take a look at the ob_start usage and use the PHPob_start () function to open the browser cache. This ensures that the cache content is completed when you call flush (), ob_end_flush () (or the program execution is completed.
First, let's take a look at the ob_start usage. use the PHP ob_start () function to open the browser cache. This ensures that the cache content is displayed after you call flush (), ob_end_flush () (or program execution is completed) the code is as follows:
-
- Ob_start (); // open the buffer
- Phpinfo (); // use the phpinfo function
- $ Info = ob_get_contents (); // Obtain the buffer content and assign it to $ info
- Using file1_fopen('info.txt ', 'w'); // open the info.txt file.
- Fwrite ($ file, $ info); // write the information to info.txt
- Fclose ($ file); // Close the info.txt file
- ?>
PHP ob_start () functions have a great feature. you can also use the ob_start parameter to automatically run commands after the cache is written, such as ob_start ("ob_gzhandler "); the most common practice is to use ob_get_contents () to get the code of the content in the cache as an example of compressing the web page. We use the ob_gzip function, use ob_start to compress the output content and put it in the "buffer zone". the code is as follows:
- // Enable compression
- If (function_exists ('OB _ gzip '))
- {
- Ob_start ('OB _ gzip ');
- }
- // Prepare some content to be compressed
- For ($ I = 0; I I <100; $ I ++)
- {
- Echo ('Here is the test content.
');
- }
- // Output compressed results
- Ob_end_flush ();
- // This is the ob_gzip compression function.
- Function ob_gzip ($ content)
- {
- If (! Headers_sent () & extension_loaded ("zlib") & strstr ($ _ SERVER ["HTTP_ACCEPT_ENCODING"], "gzip ")){
- $ Content = gzencode ($ content, 9 );
- Header ("Content-Encoding: gzip ");
- Header ("Vary: Accept-Encoding ");
- Header ("Content-Length:". strlen ($ content ));
- }
- Return ($ content );
- }