search
HomeBackend DevelopmentPHP ProblemHow to close ob_start in php

How to close ob_start in php

Nov 19, 2021 am 10:58 AM
php

php ob_start is used to open the output control buffer. If you turn off ob_start, you can use the ob_end_clean() function to silently discard the contents of the buffer.

How to close ob_start in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to close ob_start in php?

ob_start — Open the output control buffer

Description

ob_start(callable $output_callback = null, int $chunk_size = 0, int $flags = PHP_OUTPUT_HANDLER_STDFLAGS): bool

This function will open the output buffer. When output buffering is activated, the script will not output content (except http headers), instead the content to be output is stored in an internal buffer.

The contents of the internal buffer can be copied to a string variable using the ob_get_contents() function. To output the content stored in the internal buffer, you can use the ob_end_flush() function. In addition, using the ob_end_clean() function will silently discard the contents of the buffer.

WARNING

Some web servers (such as Apache) will change a script's working directory when there is a callback function being called. You can change it back in the callback function, for example chdir(dirname($_SERVER['SCRIPT_FILENAME'])) .

The output buffer is stackable, which means that when one ob_start() is active, you can call another ob_start(). Just make sure you call ob_end_flush() the correct number of times. If multiple output callbacks are active, the output will always be filtered through them in nested order.

Parameters

output_callback

Optional parameters The output_callback function can be specified. This function takes a string as argument and returns a string. This function will be called when the output buffer is flushed (emitted) or cleaned (ob_flush(), ob_clean() or similar functions); or when the output buffer contents are flushed to the browser at the end of the request. transfer. When output_callback is called, it receives the contents of the output buffer as a parameter and is expected to return a new output buffer as a result. The contents of this newly returned output buffer will be sent to the browser. If this output_callback is not a callable function, this function will return false.

The following is the callback signature:

handler(string $buffer, int $phase = ?): string

buffer

The contents of the output buffer.

phase

Bit mask PHP_OUTPUT_HANDLER_* constants.

If output_callback returns false, the original input content is sent directly to the browser.

This parameter output_callback can be avoided by directly giving a null value.

ob_end_clean(), ob_end_flush(), ob_clean(), ob_flush() and ob_start() cannot be called from a callback function. If they are called from a callback function, the resulting behavior is unspecified. If you want to delete the contents of the buffer, return a "" (empty string) from the callback function. It is also not possible to use output buffering functions like print_r($expression, true) or highlight_file($filename, true) from a callback function.

注意:
ob_gzhandler() function exists to facilitate sending gz-encoded data to web browsers that support compressed web pages. ob_gzhandler() determines what type of content encoding the browser will accept and will return its output accordingly.

chunk_size

If the optional parameter chunk_size is assigned, the buffer will be flushed after any output operation that causes the length of the buffer to be equal to or exceed chunk_size. The default value 0 means the function is called only at the end.

Prior to PHP 5.4.0, 1 was a special case value that set chunk_size to 4096 bytes.

flags

The flags parameter represents a mask bit used to control operations on the buffer. The default is to allow output buffers to be cleaned, flushed and removed, which can be set explicitly via PHP_OUTPUT_HANDLER_CLEANABLE | PHP_OUTPUT_HANDLER_FLUSHABLE | PHP_OUTPUT_HANDLER_REMOVABLE, or PHP_OUTPUT_HANDLER_STDFLAGS as shorthand. , or return false on failure.

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of How to close ob_start in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools