PHP prompts Cannotmodifyheaderinformation-headersalreadysentby solution

Source: Internet
Author: User
This article mainly introduces the solution of the PHP prompt Cannotmodifyheaderinformation-headersalreadysentby, which is a typical error in PHP program development and has great practical value, you can refer to the example in this article to learn about the solution for the PHP prompt "Cannot modify header information-headers already sent by", which is a common problem in PHP programming. This article analyzes the solution in the form of an instance. Share it with you for your reference. The specific method is as follows:

Now let's take a look at this 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 you access the php file, the system prompts "Warning: Cannot modify header information-headers already sent ".

Cause of error:

The reason is that the header of the php program is added, header ("content-type: text/html; charset = utf-8"); then the above error occurs on the page.

Because header ('content-Type: text/html; charset = UTF-8 '); no output before sending the header, no space, you need to set the header (...) remove spaces or other output items. if other files are included, check whether output exists in other files.

I checked some information online and said it was my php. an error occurred in the configuration in ini. find php. in the INI file, output_buffering is off by default. change it to on or any number, but no result is returned.

The setcookie function must be sent out before any data is output to the browser.
Based on the above restrictions, when setting the setcookie () function, we often encounter "Undefined index", "Cannot modify header information-headers already sent "... To solve the problem, the method for "Cannot modify header information-headers already sent by" is to delay the data output to the browser before the cookie is generated. therefore, you can add the ob_start () function at the beginning of the program.

The ob_start () function is used to open the buffer. for example, if there is output before the header () function, including carriage return, space, line feed, and "Header had all ready send by" errors, 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!

Solve the problem by using the following methods:

// Ob_start () before header (); // open the buffer echo \ "Hellon \"; // output header ("location: index. php "); // redirect the browser to index. php ob_end_flush (); // output all content to the browser?>

I hope this article will help you learn 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.