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
?>