Cookie Consent by Free Privacy Policy Generator ๐Ÿ“Œ PHP: The first rule of the output buffering is you do not talk about the output buffering

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š PHP: The first rule of the output buffering is you do not talk about the output buffering


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

PHP provides built-in helpers to manipulate the output buffering, but it can be tricky to debug.

What do you mean by "OB"?

I'll use "OB" for "Output Buffering" in this post.

ย Why use buffers anyway?

The OB can be convenient for various usages, like aggregating into a string different sources that output HTML in different ways, which removes the hassle of concatenating everything manually.

If you need to capture/inspect/filter the output generated by a third-party code you don't control, the OB may also help. Some frameworks use it to handle includes and other templates.

Another common trick consists of reusing the same output for different purposes, like displaying the data and sending it through other channels (e.g., emails).

In my experience, problems usually occur with nested buffers or when buffers overrun, which is hard to predict.

"Headers already sent"

You may be already familiar with the "cannot modify headers, [...] headers already sent" error, which occurs when headers are sent to the browser after the script has begun outputting data.

PHP sends the headers to the server first because of the structure of HTTP responses:

HTTP/1.1 200 OK
Vary: Accept-Encoding
Content-Type: text/html; charset utf-8

<html>
    .... etc

As a workaround, PHP developers often use the the OB to capture the data and send it in one piece to the client (e.g., the browser) afterwards. This trick is heavily used to set headers but also cookies.

ย Memory is running out?

Memory leaks sometimes happen with ob_start(), as memory usage and latency can increase if you manipulate/dump/log large the objects.

Besides, when the OB is on, built-in functions like readfile() won't present memory issues, which makes debugging harder.

Explicit vs. implicit buffering

Unless you disable it in your server configuration (e.g., php_value output_buffering off), the OB is on. It's called implicit buffering.

Using ob_start() while another ob_start() is still active would start a new level.

ย About nested buffers

The OB is stackable, which means:

  • ob_start() adds an empty buffer on top the stack
  • ob_get_contents() returns the content of the active buffer
  • ob_get_clean() removes the active buffer and returns it

As a result, you might not get what you want because the parent buffer is still active:

output is being filtered sequentially through each of them in nesting order

That's why you find the following helper in some try/catch blocks:

try {
    // some code
} catch(Exception $e) {
    // some code

    ob_end_clean();
}

IMHO, having multiple levels of buffering is not a common need and should be used with caution.

It does not mean it's pure evil, but it can impact error processing and trigger unexpected behaviors if you don't pay attention enough.

PHP outputs everything when the script ends

Handling sensitive data in the OB can be insecure as PHP writes pending buffers to the output automatically at the end of the script, which may appear convenient but could lead to nasty surprises.

It's probably a bad idea to put such data in the OB, but some developers may want to use it to obscure some errors.

The buffer size is limited

The buffer must not exceed the OB limit (4096 bytes by default). Otherwise, it would be flushed unexpectedly, and you may lose control.

ย You cannot use the OB in the OB display handlers

The ob_start() function accepts callbacks, also called handlers, as its first argument.

This callback is called when the OB is flushed. If you use ob_start() inside handlers, you will get this error:

Cannot use output buffering in output buffering display handlers

Be careful with those callbacks where the Laws of Physics may not apply.

ย Wrap up

While the OB is convenient to set cookies and headers quickly, it should not become a general rule, as there are probably better designs.

While the OB may seem appropriate for various usages, it can be tricky when you start nesting things.

Such middle layers can be powerful to capture data but only in very specific cases.

...



๐Ÿ“Œ PHP: The first rule of the output buffering is you do not talk about the output buffering


๐Ÿ“ˆ 106.16 Punkte

๐Ÿ“Œ Intel: Let's talk about SGX, baby. Let's talk about 2U and me. Let's talk about all the good things, and the bad...


๐Ÿ“ˆ 31.95 Punkte

๐Ÿ“Œ Intel: Let's talk about SGX, baby. Let's talk about 2U and me. Let's talk about all the good things, and the bad...


๐Ÿ“ˆ 31.95 Punkte

๐Ÿ“Œ Why you should never use the date validation rule without the data_format rule in Laravel


๐Ÿ“ˆ 30.26 Punkte

๐Ÿ“Œ OWASP ModSecurity Core Rule Set 3.0.2 PHP Script Upload Rule privilege escalation


๐Ÿ“ˆ 29.83 Punkte

๐Ÿ“Œ The First Rule of Security Club: Donโ€™t Talk About Security


๐Ÿ“ˆ 29.81 Punkte

๐Ÿ“Œ GDPR: One rule to rule them all โ€“ legally


๐Ÿ“ˆ 26.31 Punkte

๐Ÿ“Œ Let the Rule of Law Rule Law Enforcement: Reflections on the Current Attorney Generalโ€™s Tenure


๐Ÿ“ˆ 26.31 Punkte

๐Ÿ“Œ 6-Month Rule: Unmasking the Lux Academy 6-Month #MonkMode Rule.


๐Ÿ“ˆ 26.31 Punkte

๐Ÿ“Œ Disney Plus Not Loading? Hereโ€™s How to Fix Disney Plus Buffering Issues


๐Ÿ“ˆ 25.48 Punkte

๐Ÿ“Œ Letโ€™s Talk About ELK Baby, Letโ€™s Talk about You and AD


๐Ÿ“ˆ 25.25 Punkte

๐Ÿ“Œ Webcast: Letโ€™s Talk About ELK Baby, Letโ€™s Talk About You and AD


๐Ÿ“ˆ 25.25 Punkte

๐Ÿ“Œ First rule of Ransomware Club is do not pay the ransom, but it looks like Carlson Wagonlit Travel didn't get the memo


๐Ÿ“ˆ 24.56 Punkte

๐Ÿ“Œ Feds Unveil Rule Requiring Cars To 'Talk' To Each Other


๐Ÿ“ˆ 23.81 Punkte

๐Ÿ“Œ Feds Unveil Rule Requiring Cars To 'Talk' To Each Other


๐Ÿ“ˆ 23.81 Punkte

๐Ÿ“Œ go-ipfs up to 0.7.x Console Output escape output


๐Ÿ“ˆ 23.33 Punkte

๐Ÿ“Œ The First Rule of Microsoft Excel -- Don't Tell Anyone You're Good at It


๐Ÿ“ˆ 23.1 Punkte

๐Ÿ“Œ You better watch out, you better not cry. Better not pout, I'm telling you why: SQLite vuln fixes are coming to town


๐Ÿ“ˆ 22.65 Punkte

๐Ÿ“Œ What are Android rules and how do you create a Do Not Disturb rule?


๐Ÿ“ˆ 22.51 Punkte

๐Ÿ“Œ New output function - let's talk without a decorator in Angular


๐Ÿ“ˆ 22.32 Punkte

๐Ÿ“Œ GroรŸbritannien: Virgin Media O2 will Talk Talk kaufen


๐Ÿ“ˆ 21.3 Punkte

๐Ÿ“Œ Stรถrung bei Aldi Talk: Probleme mit mobilem Internet und mit der &quot;Mein Aldi Talk&quot;-App


๐Ÿ“ˆ 21.3 Punkte

๐Ÿ“Œ Stรถrung bei Aldi Talk: Probleme mit &quot;Mein Aldi Talk&quot; und dem mobilen Internet


๐Ÿ“ˆ 21.3 Punkte

๐Ÿ“Œ What we talk about when we talk about impostor syndrome


๐Ÿ“ˆ 21.3 Punkte

๐Ÿ“Œ Equifax is going to make you work for that 125 bucks it owes each of you: Biz sneaks out Friday night rule change


๐Ÿ“ˆ 21.05 Punkte

๐Ÿ“Œ What you should Talk About on a First Time frame


๐Ÿ“ˆ 20.6 Punkte

๐Ÿ“Œ Fredy Kuenzler: Buffering sucks! (deutsche รœbersetzung)


๐Ÿ“ˆ 20.08 Punkte

๐Ÿ“Œ Fredy Kuenzler: Buffering sucks!


๐Ÿ“ˆ 20.08 Punkte

๐Ÿ“Œ Fredy Kuenzler: Buffering sucks! (deutsche รœbersetzung)


๐Ÿ“ˆ 20.08 Punkte

๐Ÿ“Œ Fredy Kuenzler: Buffering sucks!


๐Ÿ“ˆ 20.08 Punkte

๐Ÿ“Œ How to Stop Kodi Buffering, Step-by-Step Fixes That Work


๐Ÿ“ˆ 20.08 Punkte

๐Ÿ“Œ Stop Netflix Throttling and Watch Videos Without Buffering: Quick Fixes that Work


๐Ÿ“ˆ 20.08 Punkte











matomo