output-buffering

PHP buffer why \r\n

时光怂恿深爱的人放手 提交于 2019-11-26 22:07:34
问题 I have a few conceptual questions (all related, I think) regarding the following script, at the comments. The script works fine. <?PHP ob_start(); // Create string to overflow browser buffer ...? $buffer = str_repeat(" ", 4096); // Indicate new header / html content ...? $buffer .= "\r\n<span></span>\r\n"; for ($i=0; $i<5; $i++) { echo $buffer.$i; ob_flush(); flush(); sleep(1); } ob_end_flush(); ?> First, why do I need to send the \r\n<tag>\r\n to the browser? I assume it has something to do

Calling ob_flush() and flush(), yet browser doesn't show any output until script finishes

穿精又带淫゛_ 提交于 2019-11-26 22:00:01
问题 Hi Please View Below Code : <?php ob_start(); echo "Start ...<br />\n"; for( $i = 0 ; $i < 10 ; $i++ ) { echo "$i<br />\n"; ob_flush(); flush(); sleep(1); } echo "End ...<br />\n"; ?> It's Incorrect ? i'm tested it but my output show when script is done, have any solution ? 回答1: Hey man I was also got stuck in this problem and finally got the correct solution here it is for you you have to add content type for your page you can do that by two ways 1. using html tag <meta http-equiv="Content

PHP: Output data before and after sleep()?

孤街醉人 提交于 2019-11-26 21:41:24
问题 This is purely for learning more about output buffering and nothing more. What I wish to do is echo a string to the browser, sleep 10 seconds, and then echo something else. Normally the browser would wait the full 10 seconds and then post the whole result, how I would I stop that? An example: ob_start(); echo "one"; sleep(10); echo "two"; 回答1: faileN's answer is correct in theory. Without the ob_flush() the data would stay in PHP's buffer and not arrive at the browser until the buffer is

HTML into PHP Variable (HTML outside PHP code)

萝らか妹 提交于 2019-11-26 19:12:37
问题 I am new to php and wondering if I can have something like this: <?php ... magicFunctionStart(); ?> <html> <head>...</head> <body>...</body> </html> <?php $variable = magicFunctionEnd(); ... ?> What I have to use right now is <?php ... $variable = "<html><head>...</head><body>...</body></html>" ?> Which is annoying and not readable. 回答1: Have you tried "output buffering"? <?php ... ob_start(); ?> <html> <head>...</head> <body>...<?php echo $another_variable ?></body> </html> <?php $variable =

PHP Flush/ob_flush not working

a 夏天 提交于 2019-11-26 18:59:47
I've tried several attempts at getting my flush and ob_flush to work. I've tried setting the ini to allow buffering, I've tried using several different functions I found online for output buffering, and none of it at all is working. The script wants to wait until it is completly done until it echos output. Here is the script I have so far ob_start(); //Login User echo 'Logging in to user<br>'; ob_flush(); flush(); $ch = curl_init("http://www.mysite.com/login/"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$user&pass=

Test PHP headers with PHPUnit

和自甴很熟 提交于 2019-11-26 17:58:30
问题 I'm trying to use PHPunit to test a class that outputs some custom headers. The problem is that on my machine this: <?php class HeadersTest extends PHPUnit_Framework_TestCase { public function testHeaders() { ob_start(); header('Location: foo'); $headers_list = headers_list(); header_remove(); ob_clean(); $this->assertContains('Location: foo', $headers_list); } } or even this: <?php class HeadersTest extends PHPUnit_Framework_TestCase { public function testHeaders() { ob_start(); header(

How to flush data to browser but continue executing

醉酒当歌 提交于 2019-11-26 15:55:35
问题 I have a ob_start() and a corresponding ob_flush() . I would like to flush a portion of data and continue executing the rest. Using ob_flush() didn't help. Also if possible rest needs to happen without showing loading in browser. EDIT: I don't want to use ajax 回答1: ob_flush writes the buffer. In other words, ob_flush tells PHP to give Apache (or nginx/lighttpd/whatever) the output and then for PHP to forget about it. Once Apache has the output, it does whatever it wants with it. (In other

How to clear previously echoed items in PHP

让人想犯罪 __ 提交于 2019-11-26 15:37:26
问题 In php, is there any way to clear/remove all previously echoed or printed items? For example: <?php echo 'a'; print 'b'; // some statement that removes all printed/echoed items echo 'c'; // the final output should be equal to 'c', not 'abc' ?> My script uses the include function. The included files are not supposed to echo anything. Just in case someone (ex = hacker) tries, I need a way to remove. 回答1: <?php ob_start(); echo 'a'; print 'b'; // some statement that removes all printed/echoed

PHP buffer ob_flush() vs. flush()

北慕城南 提交于 2019-11-26 12:17:08
What's the difference between ob_flush() and flush() and why must I call both? The ob_flush() reference says: This function will send the contents of the output buffer (if any). The flush() reference says: Flushes the write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc). However, it continues to say: [it] may not be able to override the buffering scheme of your web server… So, seems to me that I could just use ob_flush() all of the time. However, I get strange results when I do that. Could someone explain in simple terms what's going on here? mario ob_flush sends an

Get content between two strings PHP

℡╲_俬逩灬. 提交于 2019-11-26 08:44:02
Whats is the best way to obtain the content between two strings e.g. ob_start(); include('externalfile.html'); ## see below $out = ob_get_contents(); ob_end_clean(); preg_match('/{FINDME}(.|\n*)+{\/FINDME}/',$out,$matches); $match = $matches[0]; echo $match; ## I have used .|\n* as it needs to check for new lines. Is this correct? ## externalfile.html {FINDME} Text Here {/FINDME} For some reason this appears to work on one place in my code and not another. Am I going about this in the right way? Or is there a better way? Also is output buffer the way to do this or file_get_contents? Thanks in