Cannot modify header information error Solution

Source: Internet
Author: User

<? PHP
Ob_start ();
Setcookie ("username", "bin", time () + 3600 );
Echo "the username is:". $ http_cookie_vars ["username"]. "\ n ";
Echo "the username is:". $ _ cookie ["username"]. "\ n ";
Print_r ($ _ cookie );
?>
Warning: cannot modify header information-headers already sent by error cause
I am in PHP Program Added the header,
Header ("cache-control: No-cache, must-revalidate ");
The above error is displayed on the page, and N documents are not returned. Today, I occasionally found a problem with the configuration in my PHP. ini, and found the php. ini file under c: \ windows \.
Output_buffering is off by default. Now I can set it to 4096.
The error message is used to solve the Display Error and cannot be followed by (date + number of Exported Files) as the file name.
The setcookie function must be sent out before any information is sent to the browser.
Based on the above restrictions, when setting the setcookie () function, we often encounter "undefined Index", "cannot modify header information-headers already sent "... To solve this problem, the method of "cannot modify header information-headers already sent by" is before the cookie is generated, extend the token to the receiver. Therefore, you can add ob_start (); at the beginning 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 first use ob_start () to open the buffer PHP Code Both the data block and echo () output will enter the buffer and will not be output immediately. Of course, opening the buffer will play a lot of roles, as long as you play your imagination. You can summarize the following four points:

1. used 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
?>

2. The phpinfo () function can obtain information on the client and server, but it is best to save the client information using the buffer method.
Ob_start (); // open the buffer
Phpinfo (); // use the phpinfo Function
$ Info = ob_get_contents (); // obtain the buffer content and assign it to $ info
$ File = fopen (\ 'info.txt \ ', \ 'W \'); // open the info.txt file.
Fwrite ($ file, $ info); // write the information to info.txt
Fclose ($ file); // close the info.txt File
?>

3. Static Page Technology
Ob_start (); // open the buffer
?>
All PHP page output
$ Content = ob_get_contents (); // Retrieves all the content output on the PHP page.
$ Fp = fopen ("output00001.html", "W"); // create a file, open it, and write
Fwrite ($ FP, $ content); // write the content of the PHP page into output00001.html, and then ......
Fclose ($ FP );
?>

4. Output code
Function run_code ($ code ){
If ($ code ){
Ob_start ();
Eval ($ Code );
$ Contents = ob_get_contents ();
Ob_end_clean ();
} Else {
Echo "error! No output ";
Exit ();
}
Return $ contents;
}

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.