HP function Setcookie () Error: Warning:cannot Modify header_php Tutorial

Source: Internet
Author: User
Tags file info phpinfo
Today in the use of PHP function Setcookie () Set the value is prompt: Warning:cannot Modify header information headers already sent by mistake, immediately went to GG asked a bit, Find n Solutions I'm going to sort it out. Hope will be of some help to everyone.

At the end of the day, I see PHP talking about a friend in the learning group saying set cookies. Asked him for the code to see the reason! Error
Warning:cannot Modify header Information–headers already sent by (output started at cookie1.php:4) in cookie1.php on Li NE 5


Ob_start ();
Setcookie ("username", "Song Yan 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 sent byCause of the error
I added the header to the PHP program,
Header ("Cache-control:no-cache,must-revalidate");
After the page appears above the error, read n data also has no results. Today occasionally found that the original is my php.ini inside the configuration out of the problem, found in c:windows php.ini file
The output_buffering default is off. I'm going to set it to 4096 now OK.
Used to resolve display prompt error, cannot press (date + export file number) to file name error message.
The Setcookie function must be sent out to the browser before any material is given.
Based on the above restrictions, so? Etcookie () Letter?? R, Chang?? Buck quot; Undefined index "," Cannot modify header Information-headers already sent by "... Wait for???, solution? Q "Cannot modify header Information-headers already sent by"????? The method is before the birth cookie, the first delay? You can add Ob_start () to the front of the program,??? The/> Ob_start () function is used to open buffers, such as the header () function, if there is an output, including carriage return space, there will be "Header had all ready to send by" error, you can first use Ob_start () The data Block and Echo () output that open the buffer PHP code will go into the buffer without immediately outputting it. Of course, opening the buffer is a lot of work, just play your imagination. You can summarize the following four points:

1. Before header ()

Ob_start (); Open buffer
echo "Hellon"; Output
Header ("location:index.php"); redirect the browser to index.php
Ob_end_flush ();//output all content to the browser
?>

The 2.phpinfo () function obtains information on both the client and server side, but the way to save the client information with buffers is the best choice.
Ob_start (); Open buffer
Phpinfo (); Using the Phpinfo function
$info =ob_get_contents (); Get the contents of the buffer area and assign it to $info
$file =fopen (' Info.txt ', ' W '); Open File Info.txt
Fwrite ($file, $info); Write information to Info.txt
Fclose ($file); Close File Info.txt
?>

3. Static page Technology
Ob_start ();//Open buffer
?>
Full output of PHP page
$content = Ob_get_contents ();//Get all the contents of the PHP page output
$fp = fopen ("output00001.html", "w"); Create a file, and open it, ready to write
Fwrite ($fp, $content); Write the contents of the PHP page to output00001.html, 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;
}

Read the php manual and searched for the reason. Get a conclusion

Method One:

There are some restrictions on the use of cookies in PHP.
1, the use of Setcookie must be before the label
2. You cannot use the Echo input before using Setcookie
3. Cookies will not appear until the page is loaded
4, Setcookie must be placed in any data output browser before sending out
Due to the above limitations, when using the Setcookie () function, learn to encounter "Undefined index", "Cannot modify header Information–headers already sent by" ... The solution is to generate a cookie before outputting the content, and the function Ob_start () can be added at the top of the program;

Ob_start: Open Output buffer
function format: void Ob_start (void)
Note: When the buffer is active, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. In order to output the contents of the buffer, you can use the contents of the Ob_end_flush () or flush () output buffers.

Method Two:

Fix Warning:cannot Modify header information–headers already sent by ... Someone said to write Ob_start () at the beginning of the file;

Then open the php.ini and set the output_buffering to ON. Re-starting Appache,ok. It seems that this is the solution.
Special attention: (I just saw 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 encoded file contains the BOM reason, and php4,5 is not support BOM. Remove the BOM, you can open the conversion with notepad++. (I just saw this to solve the problem)
Use PHP's Ob_start (); Control your browser cache.

http://www.bkjia.com/PHPjc/633185.html www.bkjia.com true http://www.bkjia.com/PHPjc/633185.html techarticle today in the use of PHP function Setcookie () Set the value is prompt: Warning:cannot Modify header information headers already sent by mistake, immediately went to GG asked a bit, 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.