PHP hints cannot modify header information-headers already sent by workaround _php Tips

Source: Internet
Author: User
Tags php programming setcookie

This article describes the PHP hints cannot modify header Information-headers already sent by solution, is the PHP design process often encountered problems. In this paper, an example is used to analyze the solution. Share to everyone for your reference. The specific methods are as follows:

Now take a look at this piece of code:

<?php 
Ob_start ();
Setcookie ("username", "Test", Time () +3600);
echo "The username is:". $HTTP _cookie_vars["username"]. " \ n ";
echo "The username is:". $_cookie["username"]. " \ n ";
Print_r ($_cookie);
? >

When accessing the PHP file prompts Warning:cannot Modify header information-headers already sent by

Cause of the error:

The reason is that the head of the PHP program is added, header ("content-type:text/html; Charset=utf-8 "), and then the page appears with the above error.

Because header (' content-type:text/html;charset= UTF-8 '), no output before sending header, no space, you need to have header (...) Before the space is removed, or other output is removed, if he contains other files above, you have to check the other files have output.

On the Internet to check some information, said it is my php.ini inside the configuration out of the problem, find the php.ini file in the output_buffering default to OFF, change it to on or any number, but try to no results.

The Setcookie function must be sent out before any data is exported to the browser
Based on these limitations, the Setcookie () function is often encountered "Undefined index", "Cannot modify header Information-headers already sent by" ... And so on, the solution to the "cannot modify header Information-headers already sent by" method 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 The start () 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!

The problem is resolved by the following methods:

Ob_start () before header ()

;///Open buffer 
echo \ "hellon\";//Output 
header ("location:index.php"); redirect browser to index.php 
Ob_end_flush ()//output all content to browser 
?>

I hope this article will help you with the learning of PHP programming.

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.