Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //set headers
- header('Transfer-Encoding: chunked');
- header('Content-Type: text/html');
- //browsers collect first 1024 bytes
- //and show page only if bytes collected
- //so we will use space padding.
- //if you cannot understand what it means
- //check script with PADDING=0
- define("PADDING", 16);
- //caret return and new line characters as constant
- define("RN", "\r\n");
- //user function what get current output buffer data
- //and prefixes it with current buffer length.
- //next it call flush functions
- function flush_data(){
- $str=ob_get_contents();
- ob_clean();
- echo dechex(strlen($str)).RN.$str.RN;
- ob_flush();
- flush();
- }
- //default HTML 5 page
- echo "<!doctype html><html><head><title>Transfer-Encoding: chunked</title>";
- echo "<script>";
- //+padding
- for($i=0;$i<PADDING;$i++){
- //64 spaces (1 block)
- echo " ";
- }
- echo "</script></head><body><div>";
- echo "<h1>Chunked Output Test</h1>";
- //current output buffer will shown immediately in browser
- //after this function
- flush_data();
- //cycle wait 1 sec before next iteration
- for($i=0;$i<10;$i++)
- {
- //print iteration number
- echo "$i<br>";
- flush_data();
- usleep(250000);
- }
- echo "</div></body></html>".RN;
- //terminating part of encoding format
- flush_data();
- echo "0\r\n\r\n";
- ob_flush();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement