Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.36 KB | None | 0 0
  1. <?php
  2. //set headers
  3. header('Transfer-Encoding: chunked');
  4. header('Content-Type: text/html');
  5.  
  6. //browsers collect first 1024 bytes
  7. //and show page only if bytes collected
  8. //so we will use space padding.
  9. //if you cannot understand what it means
  10. //check script with PADDING=0
  11. define("PADDING", 16);
  12.  
  13. //caret return and new line characters as constant
  14. define("RN", "\r\n");
  15.  
  16. //user function what get current output buffer data
  17. //and prefixes it with current buffer length.
  18. //next it call flush functions
  19. function flush_data(){
  20.     $str=ob_get_contents();
  21.     ob_clean();
  22.     echo dechex(strlen($str)).RN.$str.RN;
  23.     ob_flush();
  24.     flush();
  25. }
  26.  
  27. //default HTML 5 page
  28. echo "<!doctype html><html><head><title>Transfer-Encoding: chunked</title>";
  29. echo "<script>";
  30.  
  31. //+padding
  32. for($i=0;$i<PADDING;$i++){
  33.     //64 spaces (1 block)
  34.     echo "                                                                ";
  35. }
  36. echo "</script></head><body><div>";
  37. echo "<h1>Chunked Output Test</h1>";
  38.  
  39. //current output buffer will shown immediately in browser
  40. //after this function
  41. flush_data();
  42.  
  43. //cycle wait 1 sec before next iteration
  44. for($i=0;$i<10;$i++)
  45. {
  46.     //print iteration number
  47.     echo "$i<br>";
  48.     flush_data();
  49.     usleep(250000);
  50. }
  51.  
  52. echo "</div></body></html>".RN;
  53.  
  54. //terminating part of encoding format
  55. flush_data();
  56. echo "0\r\n\r\n";
  57. ob_flush();
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement