Home > Backend Development > PHP Tutorial > How to implement BigPipe chunked output through php

How to implement BigPipe chunked output through php

jacklove
Release: 2023-03-31 10:48:01
Original
1838 people have browsed it

Principle: Use ob_flush() and flush() to output the contents of the buffer in advance, and the browser can load this part in advance content, no need to wait for all output to complete before loading.

Divide the page content into small blocks, output one and then the next, so that users can see the page content as early as possible and optimize the user experience.

First of all, the content of head should be loaded first, and static content such as css and javascript should be loaded as early as possible. Therefore, flush() should be used to output after head.

Example: First output the head static content, then divide each

into a chunk, and output it every second.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title> Big Pipe </title>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <style type="text/css">
  body{margin:0px; background:#CCCCCC;}
  p{text-align:center; margin:10px;}
  img{width:450px;}
  </style>
 </head>
 <?php cache_flush() ?>
 <body>  
  <p><img src="http://image.tianjimedia.com/uploadImages/2013/240/5CPOE4UZ2T40.jpg"></p>
  <?php cache_flush(); ?>
  <p><img src="http://image.tianjimedia.com/uploadImages/2013/240/6893CY9XEQD1.jpg"></p>
  <?php cache_flush(); ?>
  <p><img src="http://image.tianjimedia.com/uploadImages/2013/240/83H52SG02V32.jpg"></p>
 </body>
</html>
<?php
function cache_flush($sec=1){
    ob_flush();
    flush();
    usleep($sec*1000000);
}
?>
Copy after login

Issues to note:
1. Try to use one output to output as much content as possible.
2. Try to load synchronously.
3. The more chunks are divided, the better. It depends on the actual demand.
4.ob_flush() and flush() must be used at the same time, because flush() will have no effect in some cases.

This article explains how to implement BigPipe block output through php. For more related content, please pay attention to the php Chinese website.

Related recommendations:

Related introduction to mysql optimization of insert performance

##How to use PHP common customization Method

How to encrypt/decrypt files using XOR via php

The above is the detailed content of How to implement BigPipe chunked output through php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template