What to do if php header doesn't work
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!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
