Cannot modify header information error resolution _php tips

Source: Internet
Author: User
<?php


Ob_start ();


Setcookie ("username", "Song Yan Bing", 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 head of the PHP program,


header ("Cache-control:no-cache,must-revalidate");


after the page appears above the error, see N Data and no results. Today occasionally found that the original is my php.ini inside the configuration out of the problem, C:\windows\ found php.ini file

The
output_buffering defaults to OFF. I'm going to set it to 4096 now OK.


is used to resolve error messages that display prompt errors and cannot be named by (date + export number of files).

The
Setcookie function must be sent out before any data is output to the browser


is based on these limitations, so the Setcookie () function is often encountered "Undefined index", "Cannot modify header Information-headers already sent by" ... And so on, the way to solve "cannot modify header Information-headers already sent by" is to delay the data output to the browser before the cookie is generated, so you can add ob_ to the front of the program Start (), this function.


The Ob_start () function is used to open a buffer, such as the header () function, if there is output before, including carriage return \ space \ linefeed \ will have "Header had all ready send" error, you can first use Ob_start () The data Block and Echo () output of the open buffer PHP code will enter the buffer without immediate output. Of course, opening the buffer has a lot of effect, just play with your imagination. You can summarize the following four points:





1. Before header ()



Ob_start (); Open Buffer


echo \ "hellon\"; Output


header ("location:index.php"); redirect browser to index.php


Ob_end_flush ()//output all content to browser


?>




The
2.phpinfo () function obtains information on both the client and server side, but the best option is to save the client information in a buffer .


Ob_start (); Open Buffer


phpinfo (); Using the Phpinfo function


$info =ob_get_contents (); Gets the contents of the buffer area and assigns 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


?>


the entire output of the PHP page


$content = ob_get_contents ()//Get all the content 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, 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.