Php:cannot Modify header Information-headers already sent by error resolution

Source: Internet
Author: User
Tags setcookie

<?php
Ob_start ();
Setcookie ("username", "Test", Time () +3600);
echo "The username is:". $HTTP _cookie_vars["username"]. " \ n ";
echo "The username is:". $_cookie["username"]. " \ n ";
Print_r ($_cookie);
?>
Prompted Warning:cannot Modify header Information-headers already sent by error while accessing the PHP file
The reason is that the header of the PHP program has been added, headers ("content-type:text/html; Charset=utf-8 "); The above error appears on the page. On the Internet to check some information, that is my php.ini inside the configuration out of the problem, find the php.ini file output_buffering default is off, change it to on or any number, but try to have no results.

The Setcookie function must be sent out before any data is output to the browser.
Based on these limitations, when you execute the Setcookie () function, you often encounter "Undefined index", "Cannot modify header Information-headers already sent by" ... Cannot modify header information-headers already sent by "This is the wrong way 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 () function.


The Ob_start () function is used to open the buffer, such as the header () function, if there is output, including carriage return \ space \ newline \ will have "Header had all ready to send by" error, you can first use Ob_start () The data Block and Echo () output that open the buffer PHP code will go into the buffer without immediately outputting:

The problem is resolved by the following methods:

Before the header ()

Ob_start (); Open buffer
echo \ "Hellon\"; Output
Header ("location:index.php"); redirect the browser to index.php
Ob_end_flush ();//output all content to the browser
?>

Php:cannot Modify header Information-headers already sent by error resolution

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.