Methods for php header not working: 1. Delete the space between location and ":"; 2. Delete the output before using the header; 3. The PHP code after the header is executed.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
What should I do if the php header does not work?
Cause analysis and solutions for PHP Header failure
When using header("location:test.php") to jump in PHP, please pay attention to the following A few points:
1. There cannot be a space between location and the ":" sign, otherwise an error will occur.
2. There cannot be any output before using the header, and there must be no space after the tag "?>" in the include page! !
3. The PHP code after the header will also be executed.
Continued:
Problem: Input content before the header function
Generally speaking, html content cannot be output before the header function. Similar ones include setcookie() and session functions. These functions require adding message header information to the output stream. If there are statements such as echo before header() is executed, when header() is encountered later, a “Warning: Cannot modify header information – headers already sent by….” error will be reported. That is to say, there cannot be any text, blank lines, carriage returns, etc. in front of these functions, and it is best to add the exit() function after the header() function. For example, in the following incorrect writing, there is a blank line between the two PHP code segments:
//There should be a blank line here
Reason:
Start of PHP script When executed, it can send http message header (title) information and body information at the same time. The http message header (from header() or SetCookie() function) is not sent immediately, instead, it is saved to a list. This allows you to modify the header information, including the default header (such as the Content-Type header). However, once the script sends any non-header output (for example, using HTML or a print() call), then PHP must first After sending all the headers, then terminate the HTTP header. Then continue to send the body data. From this point on, any attempt to add or modify the header information is not allowed, and one of the above error messages will be sent.
Solution:
Modify php.ini to open the cache (output_buffering), change output_buffering=0 to output_buffering=4096
or use the cache function ob_start() in the program , ob_end_flush() etc. The principle is: when output_buffering is enabled, PHP does not send the HTTP header when the script sends output. Instead, it pipes this output into a dynamically growing cache (only available in PHP 4.0, which has a centralized output mechanism). You can still modify/add headers, or set cookies, since headers are not actually sent. When all scripts terminate, PHP will automatically send HTTP headers to the browser, and then send the contents of the output buffer.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if php header doesn't work. For more information, please follow other related articles on the PHP Chinese website!

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

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.

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.

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

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

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.

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor