Mais conteúdo relacionado Output Buffering Control with PHP14. Buffer Levels Level 1 Level 2 Level 3 Level 4 Server Level 3 Output to Browser Ob_start (ob_gzhandler) Ob_start (condense) Ob_start (ob_tidyhandler) Ob_start 20 Db calls cache.php If (cache.php ) readfile(cache.php) else Level 4 Tidy it up Remove junk Compression Ob_start (4heckofit) Header Top Right Menu Advertisement Left Ads BLOG Header Top Right Menu Advertisement BLOG Left Ads Bottom Bottom 15. Project 1a - Output buffering <?php // top of index.php ob_start (); // the rest of your website ob_end_flush (); ?> 16. Project 2a – Nesting 1452 Answer = <?php ob_start (); echo "1" ; ob_start (); echo "2" ; $s1 = ob_get_contents (); ob_start (); echo "3" ; $s2 = ob_get_contents (); ob_end_flush (); ob_end_clean (); echo "4" ; ob_start (); echo "5" ; ob_end_flush (); echo $s1 ; ob_end_flush (); ?> 17. Project 2b – Nesting 12452 Answer = <?php ob_start (); echo "1" ; ob_start (); echo "2" ; $s1 = ob_get_contents (); ob_flush (); ob_start (); echo "3" ; $s2 = ob_get_contents (); ob_end_flush (); ob_end_clean (); echo "4" ; ob_start (); echo "5" ; ob_end_flush (); echo $s1 ; ob_end_flush (); ?> 18. Project 2c – Nesting 1234523 Answer = <?php ob_start (); echo "1" ; ob_start (); echo "2" ; $s1 = ob_get_contents (); ob_flush (); ob_start (); echo "3" ; $s2 = ob_get_contents (); ob_flush (); ob_end_flush (); ob_flush (); ob_end_clean (); echo "4" ; ob_start (); echo "5" ; ob_end_flush (); echo $s1 . $s2 ; ob_flush (); echo "7" ; ob_end_clean (); ?> 19. Project 3a – Other functions <?php ob_start (); ob_start (); echo ob_get_length () . '- length<br>' ; echo ob_get_level () . '- level<br>' ; echo ob_get_length () . '- length<br>' ; ob_end_flush (); echo ob_get_level () . '- level<br>' ; ob_end_flush (); ?> 0- length 2- level 25- length 1- level Level 0 = no buffer 20. Project 3b – Other functions <?php ob_start (); ob_start (“ ob_tidyhandler ”); echo '<br>' ; echo '<br>' ; $s = ob_get_status ( true ); print_r ( $s ); ob_end_flush (); ob_end_flush (); ?> Array ( [0] => Array ( [chunk_size] => 0 [size] => 40960 [block_size] => 10240 [type] => 1 [status] => 0 [name] => default output handler [del] => 1 ) [1] => Array ( [chunk_size] => 0 [size] => 40960 [block_size] => 10240 [type] => 1 [status] => 0 [name] => ob_tidyhandler output handler [del] => 1 ) ) 21. Project 3c – Other functions <?php ob_start ( "ob_gzhandler" ); ob_start ( “condense” ); ob_start ( 'ob_tidyhandler' ); ob_start (); $s = ob_list_handlers (); print_r ( $s ); ob_end_flush (); ob_end_flush (); ob_end_flush (); ob_end_flush (); ?> Array ( [0] => ob_gzhandler [1] => ob_tidyhandler [2] => default output handler ) 22. Project 4a – Output to file <?php ob_start ( "ob_gzhandler" ); // dynamic content // check database if page has been modified $cachefile = $_SERVER [ 'DOCUMENT_ROOT' ]. "/cache/cache09.php" ; if ( $pagemodified != 'Y' && file_exists ( $cachefile )){ readfile ( $cachefile ); } else { // content to be cached $fp = fopen ( $cachefile , 'w' ); fwrite ( $fp , ob_get_contents ()); @ chmod ( $fp , 0755 ); ob_end_flush () } // more dynamic content ob_end_flush () ?> 23. Project 4b – Cache file system <?php $cacheFile = 'cache.html' ; if ( ( file_exists ( $cacheFile )) && (( fileatime ( $cacheFile ) + 600 ) > time ()) ) { $content = file_get_contents ( $cacheFile ); echo $content ; } else { ob_start (); // write content echo 'What Ever You Want!' ; $content = ob_get_clean (); file_put_contents ( $cacheFile , $content ); echo $content ; } ?> 24. Project 5 – rewrite variable <?php output_add_rewrite_var ( 'var' , 'value' ); echo '<a href="file.php">link</a> <a href="http://example.com">link2</a>' ; echo '<form action="script.php" method="post"> <input type="text" name="var2" /> </form><br>' ; print_r ( ob_list_handlers ()); ob_end_flush (); output_reset_rewrite_vars (); echo '<br><a href="file.php">link</a> <a href="http://example.com">link2</a>' ; echo '<form action="script.php" method="post"> <input type="text" name="var2" /> </form><br>' ; print_r ( ob_list_handlers ()); ?> <a href="file.php?var=value">link</a> <a href="http://example.com">link2</a> <form action="script.php" method="post"><input type="hidden" name="var" value="value" /> 25. Project 6 – Less HTML less readable <?php function condense ( $buffer ){ // change new lines and tabs to single spaces $buffer = str_replace ( array ( "" , "" , "" , "" ), ' ' , $buffer ); // multispaces to single... $buffer = ereg_replace ( " {2,}" , ' ' , $buffer ); // remove single spaces between tags $buffer = str_replace ( "> <" , "><" , $buffer ); // remove single spaces around $buffer = str_replace ( " " , " " , $buffer ); $buffer = str_replace ( " " , " " , $buffer ); return $buffer ; } ob_start ( " condense " ); // your website ob_end_flush (); ?> Can Be Very Costly 26. Project 7 – Tidyhandler <?php ob_start ( 'ob_tidyhandler' ); echo '<p>test</i>' ; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN"> <html> <head> <title></title> </head> <body> <p> test </p> </body> </html> http://ditio.net/2008/01/03/is-your-html-code-tidy/ Uncomment in php.ini 4 xampp ;extension=php_sybase_ct.dll ;extension=php_threads.dll extension=php_tidy.dll ;extension=php_timezonedb.dll ;extension=php_translit.dll 27. Project 8 – Compression level <?php ini_set ( 'zlib.output_compression_level' , 8 ); ob_start ( "ob_gzhandler" ); // html ob_end_flush (); ?> 31. Questions Sorry, we do not answer questions If you come across this document online, please refer to www.php.net or use a search engine to find your answers. Else if you catch me standing in front of you, then I will do my best to answer your questions.