Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What if there is a Cannot modify header information-headers already sent by error in setcookie?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what to do about Cannot modify header information-headers already sent by errors in setcookie. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

The code is as follows:

Run with warning Warning: Cannot modify header information-headers already sent by

Here are some suggestions.

Method 1:

There are some restrictions on the use of Cookie in PHP.

1. Setcookie must be used before the label

2. Before using setcookie, you cannot use echo to enter content

3. Cookie will not appear until the web page is loaded.

4. Setcookie must be placed in front of any data output browser before it is sent.

.

Due to the above limitations, learn to encounter "Undefined index" and "Cannot modify header information-headers already sent by" when using the setcookie () function. The solution is to generate cookie before outputting the content, and you can add the function ob_start () to the top of the program

Ob_start: open output buffer

Function format: void ob_start (void)

Note: when the buffer is activated, all non-header information from the PHP program is not sent, but is saved in the internal buffer. To output the contents of the buffer, you can use ob_end_flush () or flush () to output the contents of the buffer.

Method 2:

Solve Warning: Cannot modify header information-headers already sent by. A few days ago, I installed a test of php's big head sticker system, and found that it reported an error Warning: Cannot modify header information-headers already sent by.

Today, I installed openads again, and this problem still occurs. Angry. After searching the Internet for a long time, some people said that they should write at the beginning of the document.

Ob_start ()

Failed.

Then open php.ini and set output_buffering to on. Restart appache,OK. It seems that this is the solution.

Special attention: (this is what I read to solve the problem)

If you use utf-8 encoding, be sure to remove the BOM from UTF-8, all because of the bom contained in the utf-8 encoding file, and php4,5 does not support bom. To get rid of bom, you can use Notepad++ to open the conversion. (that's what I saw to solve the problem.)

Solve the problem on your own

PHP4,5 does not support BOM in utf-8 encoded files.

If the file contains BOM, an error will be reported when setting cookie or session.

How to quickly and easily remove the BOM of the file.

It is recommended to use Notepad++ software, which is excellent free software.

1. You can create a new UTF-8 by default (no BOM) in Settings-- > preferences-- >

two。 If you created a new php file somewhere else, open it in notpad++ how to convert the encoding to UTF-8 (no BOM)

Format-> UTF-8 (no BOM) then you can transcode.

This is the end of the article on "what to do if there are Cannot modify header information-headers already sent by errors in setcookie". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report