Solution to the Cannotmodifyheader error in PHPHeader)

Source: Internet
Author: User
When I output XML using PHP today, I used the Header () function and also needed to echo a variable, which produced a conflict. Google, find several solutions, translate: Ifyougotthismessage: Warning: Cannotmodifyheaderinformation

When I output XML using PHP today, I used the Header () function and also needed to echo a variable, which produced a conflict. Google, find several solutions, translate: If you got this message: "Warning: Cannot modify header information-headers already sent ....

When I output XML using PHP today, I used the Header () function and also needed to echo a variable, which produced a conflict. Google, find several solutions, translate: If you got this message: "Warning: Cannot modify header information-headers already sent .... "if you see this Warning when executing the php program:" Warning: Cannot modify header information-headers alreadysent .... "Few notes based on the following user posts: There are several solutions: Blank lines (blank lines): Make sure no Blank line after Of the calling php script. Check whether There are no blank lines at the end, especially the include or require values. Many problems are caused by these blank rows. Use exit statement (solved by exit): Use exit after header statement seems to help some people add exit (); view sourceprint? 1 header ("Location: xxx"); 2 exit (); PHP has this annoying problem, if your HTML goes beforeany PHP code or any header modification before redirecting to certain page, it'll said "Warning: Cannot modify header information-headers alreadysent .... "Basically anytime you output to browser, the header is setand cannot be modified. so two ways to get around the problem: Use Javascript (with Javascript) : View sourceprint? 1 Since it's a script, it won't modify the header until execution of Javascript. You can use Javascript to replace the header. However, the above Code is not successfully executed... You need to note that using this method requires the browser to support Javascript. Use output buffering (solved by the output cache): view sourceprint? 1 2.. HTML codes... 3 This will save the output buffer on server and not output to browser yet, whichmeans you can modify the header all you want until the ob_end_flush () statement. this method is cleaner than the Javascript since Javascriptmethod assumes the browser has Javascript turn on. however, there are overheadto store output buffer on server before output, but with modern hardware iwocould imagine it won't be that Big of deal. javascript solution wowould bebetter if you know for sure your user has Javascript turn on their browser. as in the code above, this method is cached when the page is generated, so that the header can be output after the head is output. I am using this method to solve the header problem. Set output_buffering = On in php. ini (enable php. output_buffering in ini) set output_buffering = On will enable output buffering for all files. but thismethod may slow down your php output. the performance of this method depends onwhich Web server you're working with, and what kind of scripts you're using. this method is the same as the previous method in theory. However, This method enables the output cache of all php programs, which may affect the php Execution efficiency, depending on the server performance and Code complexity. Generally, html content cannot be output before the header function. For example, there are setcookie () and session functions. These functions need to add message header information to the output stream. If an echo statement exists before the header () is executed, When header () is encountered later, "Warning: Cannot modify header information-headers already sent .... "error. That is to say, there cannot be any text, blank lines, and carriage return before these functions, and it is best to add the exit () function after the header () function. For example, the following error code contains an empty line between two php code segments: view sourceprint? 1 4 // Here it should be a blank line 5 The reason is: when the PHP script starts to be executed, it can simultaneously send the http message header (title) information and subject information. 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 title information, including the default title (for example, Content-Type title ). however, once the script sends any non-title output (for example, called using HTML or print (), PHP must first send all headers and then terminate HTTPheader. then send the subject data. at this time, any attempt to add or modify the Header information is not allowed, and one of the above error messages will be sent. You can use the following methods to modify the configuration. Modify php. ini to open the cache (output_buffering), or use the cache functions ob_start () and ob_end_flush () in the program. Principle: When output_buffering is enabled, PHP does not send HTTPheader when the script sends the output. Instead, it inputs the output to the dynamically added cache through the pipeline (pipe) (which can only be used in PHP4.0 and has a centralized output mechanism ). You can still modify/Add the header or set the cookie because the header is not actually sent. When all scripts are terminated, PHP automatically sends the HTTP header to the browser, and then sends the content in the output buffer. From: http://www.nowamagic.net/php/php_CannotModifyHeader.php

Original article address: how to solve the Cannot modify Header error in PHP header.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.