PHP: Cannotmodifyheaderinformation

Source: Internet
Author: User
PHP: Cannotmodifyheaderinformation 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 );
?>
When accessing the php file, the system prompts "Warning: Cannot modify header information-headers already sent by" error cause.
The reason is that the header of the php program is added, header ("content-type: text/html; charset = utf-8"); then the above error occurs on the page. I checked some information online and said it was my php. an error occurred in the configuration in ini. find php. in the INI file, output_buffering is off by default. change it to on or any number, but no result is returned.

The setcookie function is required? Before outputting any data to a browser, send it
Based on the above? Some restrictions, so? When you use the row setcookie () function, you will often encounter "Undefined index", "Cannot modify header information-headers already sent "... ? "Cannot modify header information-headers already sent "???? The method is to delay the data output to the browser before the cookie is generated. Therefore, you can add the ob_start () letter to the front of the program ?.


The ob_start () function is used to open the buffer. for example, if there is output before the header () function, including carriage return, space, line feed, and "Header had all ready send by" errors, in this case, you can use ob_start () to open the data block of the PHP code in the buffer zone and echo () to output the data in the buffer zone rather than immediately:

Solve the problem by using the following methods:

// Before header ()

Ob_start (); // open the buffer
Echo \ "Hellon \"; // output
Header ("location: index. php"); // redirects the browser to index. php.
Ob_end_flush (); // output all content to the browser
?>

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.