How to fix Headers already sent error in PHP

0 votes

When running my script, I am getting several errors like this:

Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23
The lines mentioned in the error messages contain header() and setcookie() calls.

What could be the reason for this and how to fix it?

Feb 17, 2022 in Others by Soham
• 9,710 points
7,208 views

1 answer to this question.

0 votes

The functions that send and modify the HTTP headers must be invoked before any output is made, failing by which the call fails: Please note that a user cannot modify header information - headers already sent in which the output started at script:line

Some functions modifying the HTTP header are:

  • header / header_remove

  • session_start / session_regenerate_id

  • setcookie / setrawcookie

The Output can be Unintentional which includes the whitespace before <?php or after ?>, the UTF-8 Byte Order Mark specifically along with the previous error messages or notices. The Output can also be Intentional which includes print, echo and other functions producing output along with raw <html> sections prior <?php code. This occurs as the PHP scripts mainly generate HTML content, but also pass a set of HTTP/CGI headers to the webserver:
 

HTTP/1.1 200 OK
Powered-By: PHP/5.3.7 
Vary: Accept-Encoding 
Content-Type: text/html; charset=utf-8 

<html><head><title>PHP page output page</title></head> 
<body><h1>Content</h1> <p>Some more output follows...</p> 
and <a href="/"> <img src=internal-icon-delayed> </a>

The page/output always follows the headers. PHP has to pass the headers to the web server first. It can only do that once. After the double line break it can nevermore amend them.

When PHP receives the first output (print, echo, <html>) it will flush all collected headers. Afterward it can send all the output it wants. But sending further HTTP headers is impossible then.

answered Feb 17, 2022 by Aditya
• 7,680 points

Related Questions In Others

0 votes
1 answer

How to fix a circular reference error by if condition in excel file?

Circular reference in this context refers to ...READ MORE

answered Nov 21, 2022 in Others by narikkadan
• 63,600 points
741 views
0 votes
1 answer

How to echo WP SEO Yoast synonyms in PHP?

 the meta_key in wp_postmeta table is _yoast_wpseo_keyword ...READ MORE

answered Feb 10, 2022 in Others by narikkadan
• 63,600 points
1,145 views
0 votes
1 answer

How to echo WP SEO Yoast synonyms in PHP?

Title: "<?php echo WPSEO_Meta::get_value("title"); ?>"<br /> Description: "<?php ...READ MORE

answered Feb 24, 2022 in Others by narikkadan
• 63,600 points
728 views
0 votes
1 answer

How to create Dropdown list in excel using php

Try this: $objValidation = $objPHPExcel->getActiveSheet()->getCell('B'.$i)->getDataValidation(); $objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_LIST ); $objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_INFORMATION ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,600 points
2,470 views
0 votes
1 answer

Simple question regarding PHP, Wordpress & Yoast SEO

the way the search engine displays your ...READ MORE

answered Feb 14, 2022 in Others by narikkadan
• 63,600 points
518 views
0 votes
0 answers

PHP header(Location: ...): Force URL change in address bar

I have a new webpage but the ...READ MORE

Jun 1, 2022 in PHP by Kichu
• 19,040 points
3,650 views
0 votes
0 answers

PHP generate file for download then redirect

I have a PHP app that creates ...READ MORE

Jun 20, 2022 in PHP by narikkadan
• 63,600 points
1,019 views
0 votes
0 answers

PHP page redirect

I am working on a function where ...READ MORE

Jun 23, 2022 in PHP by narikkadan
• 63,600 points
834 views
0 votes
1 answer

How do I get the current date and time in PHP?

The time would go by your server ...READ MORE

answered Feb 16, 2022 in Others by Aditya
• 7,680 points
726 views
0 votes
1 answer

How to redirect to another page using PHP

You could use a function which is ...READ MORE

answered Feb 18, 2022 in Others by Aditya
• 7,680 points
5,352 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP