Cannot modify header Information-headers already sent by error _php tutorial

Source: Internet
Author: User
Tags file info
Summary, the ultimate approach: Modify the php.ini file, the output_buffering default is off, change to 4096.

!--? php
ob_start ();
Setcookie ("username", "Song Yan 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
I added the header to the PHP program,
Header (" Cache-control:no-cache,must-revalidate ");
After the page appears above the error, see the n data also has no results. Today occasionally found that the original is my php.ini inside the configuration out of the problem, under C:\windows\ found php.ini file
output_buffering default is off. I'm going to set it to 4096 now OK. The
is used to resolve display prompt errors and cannot press the error message (date + export file number) to the file name. The
Setcookie function must send out
based on the above restrictions before any information is sent to the browser, so Setcookie () functions often encounter "Undefined index", "Cannot modify header". Information-headers already sent by "... And so on, solving the "cannot modify header Information-headers already sent by" This is the wrong way to produce a cookie before the cache input to the browser, so you can add ob_ in front of the program Start (); this function. The
Ob_start () function is used to open buffers, such as the header () function, if there is output, including carriage return \ space \ newline \ will have "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;
}


http://www.bkjia.com/PHPjc/477179.html www.bkjia.com true http://www.bkjia.com/PHPjc/477179.html techarticle Summary, the ultimate approach: Modify the php.ini file; the output_buffering default is off, change to 4096. PHP Ob_start (); Setcookie (username, Song Yan, Time () +3600) ; echo the ...

  • 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.