How to completely eliminate Warning:cannot add header information-headers already sent in ...

Source: Internet
Author: User
Tags function prototype php script setcookie


Turn from: http://blog.csdn.net/jsyzzcx/archive/2008/05/25/2480076.aspx



Reference: http://hi.baidu.com/mikeking20031/blog/item/6a58b35113edc81e377abe5b.html





This article discusses how to completely eliminate the Warning:cannot add header information-headers already sent in ... This is a puzzling mistake.

As long as you have written PHP code, I believe you have encountered this most of the time is not warning it. We're going to take care of it today .....

Read the PHP manual, the answer is as follows:

Message "Warning:cannot send session Cookie-headers already sent ... "or" Cannot add header information-headers already sent ... ”。

function header (), Setcookie (), and the session function need to add header information to the output stream. But header information can only be sent before any other output. You cannot have any output (such as HTML) before using these functions. The function headers_sent () is able to check if your script has sent header information. See Output control functions.

Do not use the above function before there are any text, blank lines, carriage returns, spaces and so on. But... The problem is that the answer is not satisfactory. Because often the program in other PHP environment is normal operation.

First of all: How did this error arise? Let's take a look at how PHP handles HTTP header output and body output.

When the PHP script starts executing, it can send header (header) and principal information at the same time. Header information (from the 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 headings (such as content-type headings). However, once the script sends any non-caption output (for example, using HTML or print () calls), PHP must first send all headers and then terminate the HTTP header. And then continue sending the principal data. From this point on, any attempt to add or modify header information is disallowed and sends one of the above error messages.

Well, then we'll fix it:

Stupid method: Do not show the error warning all!
The concrete method does not say the plan ^_^

Solution:

1 is suitable for editing PHP with permission. INI's People

Open PHP. INI file (you try to test your PHP better than I do). Where is the INI), find

output_buffering = change to On or any number. If it is IIS6, be sure to ON, otherwise your PHP efficiency will be very slow.

2 Use a virtual host, you cannot edit PHP. INI, what to do.

Simple:

Create one in the root directory of your space. htaccess file, which reads as follows:

AllowOverride All
Php_flag output_buffering on

The unfortunate situation is: still not. All pages are not displayed.

Well, you can call and scold a space trader, and then let him give you Apache's. htaccess allowoverride Open

3 in the PHP file to solve

Ob_start ()
Enable the output buffering mechanism. Output Buffering supports multiple tiers-for example, the Ob_start () function can be called multiple times.

Ob_end_flush ()
Sends output buffer (output buffering) and disables the output buffering mechanism.

Ob_end_clean ()
Clears output buffer but does not send, and disables output buffering.

Ob_get_contents ()
Returns the current output buffer to a string. Allows you to process any output that the script emits.

Principle:

When output_buffering is enabled, PHP does not send HTTP headers when the script sends output. Instead, it enters the output through the pipeline (pipe) into the dynamically incremented cache (only PHP 4). 0, it has a centralized output mechanism). You can still modify/add headers, or set cookies, 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 contents of the output buffer. reference resolves the method of Warning:cannot modify header information-headers already sent by
In the background management or sometimes in the forum, click on a page, the top of the page will appear
Warning:cannot Modify header Information-headers already sent by ....
This type of statement is caused by the problem of the Setcookie statement.

The cookie itself has some restrictions on its use, such as:
1. Call Setcookie must be placed before 2. Do not use echo before calling Setcookie
3. Cookies will not appear in the program until the Web is downloaded
The 4.setcookie function must be sent before any data is output to the browser
5 .....
Based on these limitations, the Setcookie () function is often encountered "Undefined index", "Cannot modify header Information-headers already sent by" ... And so on, the way to solve "cannot modify header Information-headers already sent by" is to delay the data output to the browser before the cookie is generated, so you can add ob_ to the front of the program Start (), this function. This will solve the problem.

Reinstall, the background of a clear cache appears as follows, the problem is the header function. Reference notice:uninitialized string offset:1 in Virtual host absolute path/shop/includes/cls_mysql.php on line 119

Warning:cannot Modify header Information-headers already sent by (output started at Virtual host absolute path/shop/includes/cls_mysql.ph p:119) in Virtual host absolute path/shop/admin/includes/init.php on line 293

Warning:cannot Modify header Information-headers already sent by (output started at Virtual host absolute path/shop/includes/cls_mysql.ph p:119) in Virtual host absolute path/shop/admin/includes/init.php on line 294

Warning:cannot Modify header Information-headers already sent by (output started at Virtual host absolute path/shop/includes/cls_mysql.ph p:119) in Virtual host absolute path/shop/admin/includes/init.php on line 295

Warning:cannot Modify header Information-headers already sent by (output started at Virtual host absolute path/shop/includes/cls_mysql.ph p:119) in Virtual host absolute path/shop/admin/includes/init.php on line 296

Warning:cannot Modify header Information-headers already sent by (output started at Virtual host absolute path/shop/includes/cls_mysql.ph p:119) in Virtual host absolute path/shop/admin/includes/init.php on line 297

Warning:cannot Modify header Information-headers already sent by (output started at Virtual host absolute path/shop/includes/cls_mysql.ph p:119) in Virtual host absolute path/shop/includes/smarty/smarty.class.php on line 835 reference to the original post by the Wind Piaochen Yi in 2007-8-15 23:59 published
notice:uninitialized string offset:1 in Virtual host absolute path/shop/includes/cls_mysql.php on line 119
Warning:cannot Modify header Information-headers already sent by (outputstarted at Virtual host absolute path/shop/includes/cls_mysql.php : 119) in Virtual host absolute path/shop/includes/init.php on line 133
Warning:cannot Modifyheader Information-headers already sent by (output started at Virtual host absolute path/shop/includes/cls_mysql.php : 119) in Virtual host absolute path/shop/includes/init.php on line 134
Warning:cannot Modifyheader Information-headers already sent by (output started at Virtual host absolute path/shop/includes/cls_mysql.php : 119) in Virtual host absolute path/shop/includes/lib_main.php on line 891
Warning:cannotmodify header Information-headers already sent by (output started at Virtual host absolute path/shop/includes/cls_mysql.php : 119) in Virtual host absolute path/shop/includes/smarty/smarty.class.php on line 835
I don't know why my "big pig" was banned, and it's been PM to Liry, and there's no reply.
The above I reply to the content is about the BOM signature ~ ~ Read your post and found that you have carefully checked, and you also download and reinstall the
I read the code in the Error tip section:
/shop/includes/cls_mysql.php on line 119

QUOTE:
$result = mysql_query ("Show VARIABLES like ' Basedir '", $this->link_id);
$row = Mysql_fetch_assoc ($result);
if (!empty ($row [']{1}] && $row [' value ']{1} = = ': ' &&!empty ($row [' Value ']{2}] && $row [' Value ']{2} = = '//'
{
$this->platform. = ' WINDOWS ';
}
/shop/includes/init.php on line 133
/shop/includes/init.php on line 134

[Copy to Clipboard] [ - ] CODE:
Header (' cache-control:private ');
Header (' content-type:text/html; Charset=utf-8 ');
/shop/includes/lib_main.php on line 891

QUOTE:
Setcookie (' ecs[visit_times] ', $visit _times, time () + 3600 * 24 * 365, '/');
As for smarty, there should be no mistake,
From Part two and part three, it may be because of header and cookie reasons
And the first part of show's function prototype is

QUOTE:
Show [GLOBAL | Session] VARIABLES [as ' pattern ']
Related explanations:
Show VARIABLES shows some of the MySQL system variable values. This information can also be obtained by running the command mysqladmin variables.
GLOBAL and session options are available from the MySQL 4.0.3 start. If it is session, the value of the variable used in each new connection is obtained. If it is
Session, the value of the variable in the current connection is obtained. If you do not add any options, the session is the default. Local is the same as session.
Here again to use the session ~

There must be some problems with the setting of the space, and the estimate can be fixed by modifying the code

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.