Hp function setcookie () error: Warning: Cannotmodifyheader_PHP tutorial-php Tutorial

Source: Internet
Author: User
The hp function setcookie () reports the following error: Warning: Cannotmodifyheader. Today, when using the php function setcookie () to set the value, the following error occurs: Warning: Cannotmodifyheaderinformationheadersalreadysentby. go to gg and ask about the error. Find N solutions. Today, we are using the php function setcookie () when the value is set, the following error occurs: Warning: Cannot modify header information headers already sent by. go to gg and ask immediately, find N solutions. I will sort them out below, hoping to help you.

When I got off work, I saw a friend in the php discussion and learning group talking about setting cookies. Asked him for the code to see why! Error
Warning: Cannot modify header information-headers already sent by (output started at cookie1.php: 4) in cookie1.php on line 5


Ob_start ();
Setcookie ("username", "bin", time () + 3600 );
Echo "the username is:". $ HTTP_COOKIE_VARS ["username"]. "";
Echo "the username is:". $ _ COOKIE ["username"]. "";
Print_r ($ _ COOKIE );
?>
Warning: Cannot modify header information-headers already sentError 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 in 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 is required? In any? Expected? Before exiting the browser, send
Based on the above? Some restrictions, so? Why? Etcookie () letter ?? R, often ?? Why? Quot; Undefined index "," Cannot modify header information-headers already sent "... Wait ???, Solution? Q "Cannot modify header information-headers already sent "????? The method is in? Before sending a cookie, extend it first ?? Expected? Outbound? G? Therefore, you can add ob_start ();??? Letter? Idle? Br/> The ob_start () function is used to open the buffer. for example, if there is an output before the header () function, including the carriage return space and line feed, there will be an error of "Header had all ready send, 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"); // 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
Using file1_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;
}

I read the PHP manual and searched for the reason. Conclusion:

Method 1:

There are some restrictions on the use of cookies in PHP.
1. to use setcookie, you mustBefore tag
2. you cannot use echo to input content before using setcookie.
3. the cookie will not appear until the webpage is loaded
4. setcookie is sent only when it is placed in any data output browser.
Due to the above restrictions, when using the setcookie () function, you will learn to encounter "Undefined index", "Cannot modify header information-headers already sent "... The solution is to generate a cookie before the output content. you can add the function ob_start () at the top of the program ();

Ob_start: Open the output buffer.
Function format: void ob_start (void)
Note: When the buffer zone is activated, all non-file header information from the PHP program is not sent, but stored in the internal buffer zone. To output the buffer content, you can use ob_end_flush () or flush () to output the buffer content.

Method 2:

Warning: Cannot modify header information-headers already sent ...... Some people say they want to write ob_start (); at the beginning of the file.

Then open php. ini and set output_buffering to on. Restart appache, OK. It seems that this is the solution.
Special Note: (I just read this to solve the problem)

If you use UTF-8 encoding, be sure to remove the BOM in the UTF-8, this is because the UTF-8 encoding file contains the bom, and php4, 5 are not supported bom. Remove bom and use Notepad ++ to open the conversion. (I just read this to solve the problem)
Use PHP's ob_start (); to control your browser cache.

Warning () is set to the prompt: Warning: Cannot modify header information headers already sent by is wrong, go to gg to ask immediately, find N solutions below...

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.