Cannot modify header information-headers already sent by error cause

Source: Internet
Author: User
<? PHP ob_start (); setcookie ("username", "Courier", 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 added the header of the PHP program,
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 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. of course, opening a buffer has a lot to do, as long as you can make full use of your imagination. the following four points can be summarized:

1. used before header ()

 

Ob_start (); // open the buffer echo \ "hellon \"; // output header ("Location: Index. PHP "); // redirect 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 fwrite ($ file, $ info) file ); // write the information to info.txt fclose ($ file); // close the info.txt file?>

3. Static Page Technology
Ob_start (); // open the buffer

?> Full PHP page output $ content = ob_get_contents (); // get all PHP page output content $ fp = fopen ("output00001.html", "W "); // create a file, open it, and write it into fwrite ($ FP, $ content); // write the content of the PHP page to output1_1.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.