Perfect solution to the Cannotmodifyheaderinformation problem in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags ultraedit
It perfectly solves the Cannotmodifyheaderinformation problem in PHP. I have encountered this problem. if I find this solution on the internet, my friends who have added PHP to my favorites must have encountered the following problem: when the header function is used to change the http protocol header, if you find this solution on the Internet, you can add it to your favorites.
Friends who write PHP must have encountered the following problem: when the http header is changed through the header function, a warning similar to the following format will appear:

The code is as follows:


Warning: Cannot modify header information-headers already sent


Why? Before using the header function, you cannot output anything, including echo, var_dump, and other output functions.

See the test code:

The code is as follows:


Header ("content-type: text/html; charset = utf-8 ");
Die ();
?>


This code is a simple header.

Open this file with ultraEdit and switch to hexadecimal mode. we can see that there is EFBBBF at the beginning of the text, followed by 3C 3F 70 68 70 (


What is EFBBBF? Its name is BOM and Byte Order Mark, which is a convention in Windows to Mark whether the text is Unicode. The notepad program uses these three bytes to determine whether the file is ANSI or Unicode.

After knowing the cause of the problem, we can try to remove the BOM. Use Dreamweaver to open the php page, and then enter the page settings (press Ctrl + J). The following window is displayed:


Deselect "include Unicode signature (BOM)" and click OK. Now, use UltraEdit to open the hexadecimal content of the file, as shown in figure



As you can see, the BOM information is no longer available, and the file starts with 3C3F70.

Finally, let's refresh the browser and find that there is no Warning prompt. The problem is solved.

My friends who wrote PHP by ghost must have encountered the following problem: when the header function is used to change the http header, it will...

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.