0

Using an output buffer requires the server to store the entire output of the PHP in RAM, so if I have a large page, I'll wind up using a fair amount of memory - and the server will also have to wait until the entire page is generated before sending it out, which could cause a small delay. that's right ?

I don't want to know the advantage of using ob_start();. My problem is redirecting and this error: Headers already sent.

So for solving that problem, I used of ob_start(); in the fist of my codes. something like this:

<?php ob_start(); ?> 
<?php
       // 500 lines of code is here
       header(Location: www.example.com/test.php);
?> 
<html> 
      // 1000 lines of code is here
</html> 
<?php ob_end_flush(); ?>

Now my problem has been solved, just I want to know everything is ok ? my codes are optimized ? If my requests rise, my site does not delay ?

thanks

Shafizadeh
  • 9,960
  • 12
  • 52
  • 89
  • 1000 lines of normal-ish HTML might amount to 50 kilobytes. If your server is so highly constrained that 50kb is going to kill the system, then you should seriously think about upgrading servers... – Marc B Jul 14 '15 at 16:22
  • create a big page, and write a script that requests this page a few times, and make sure the requests all get handled and not blocked by some spam filter or so. this way you can test it yourself, which is the most reliable (or maybe only real) way to know if it works. – hoijui Jul 14 '15 at 16:22
  • @MarcB the ram of my server is 1GB, my requests are 2000 per hour. then should I upgrading my server ? and my main question is about `ob_start()`; using of it is causes slowing ? – Shafizadeh Jul 14 '15 at 16:40
  • like I said, 2000*50 = ~97 megabytes. if you can't handle that much, then start worying. – Marc B Jul 14 '15 at 16:41
  • @MarcB ha ha ..! why always you use of `if` ? just tell me using of `ob_start();` is good or bad or does not matter for me ? thanks ! – Shafizadeh Jul 14 '15 at 16:44
  • how should we know? You have to benchmark/test YOUR code to decide. ob_start() is a tool. whether that tool is appropriate for you is NOT something we can say yeah/nay to. if you slam your finger with a hammer, it's not the hammer's fault... – Marc B Jul 14 '15 at 16:45
  • @MarcB alright .. just tell me what can I do for releasing the Ram ? using of `ob_end_flush();` is good ? or it is ineffective ? – Shafizadeh Jul 14 '15 at 17:03
  • it'll empty the buffer, but the ram used by the buffer will still be there. you'd have to wait for the php garbage collector to kick in, or just let the script exit. php cleans up everything at exit anyways. – Marc B Jul 14 '15 at 17:09
  • @MarcB ah I see, then should I use `exit()` after `header(location: ..);` – Shafizadeh Jul 14 '15 at 17:11
  • php would exit anyways, unless you've got some wonky code that goes into an infinite loop or something. – Marc B Jul 14 '15 at 17:13
  • You should _always_ use `die()` after a `header("Location: ")` call. If you don't, the code you don't want to be executed will be executed anyway. What @MarcB says about PHP exiting anyway is technically true. However, it'll only exit after the web server notices that the client isn't listening anymore. Which could be at any point during the code execution, or even after it's completed it all. In short you will never know what happens with your code without `die()`. – ChristianF Jul 15 '15 at 07:05
  • @ChristianF I see, thanks a lot ..! – Shafizadeh Jul 15 '15 at 07:08

2 Answers2

2

The proper solution to the "Headers already sent" problem is described in a previous thread.

Basically, the correct cause of action is to move all of the processing code above any output to the browser. Then simply echo out the results, as needed, in between the HTML code.
Not only will you notice an improvement in the resource usage of the page, but you'll also notice that it will become a whole lot easier to actually read and write the code.

If the output branches are complex enough, which means anything above a very basic script (simple guestbook, etc), a template engine might be well worth the time and effort to look at.

Community
  • 1
  • 1
ChristianF
  • 2,068
  • 9
  • 14
  • ops' not asking about OB itself. he's asking about ram usage DUE to using OB. – Marc B Jul 14 '15 at 17:11
  • I quote: "I don't want to know the advantage of using ob_start();. My problem is redirecting and this error: Headers already sent." <- I prefer solving the underlying problems properly, instead of using workarounds. In this case, solving the underlying problem make the question stated moot. – ChristianF Jul 14 '15 at 17:43
0

Output buffering is frequently used and I wouldn't worry about this. For example, this SO webpage takes up ~ 64 KiB, meaning 16384 of these pages fit in 1 GiB ram simultaneously.

Probably offtopic, but if you're going to send a Location header, do you even need to execute all the other code? You could just send the header and exit() immediately.

Sjon
  • 4,989
  • 6
  • 28
  • 46