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 in Others by Soham
• 9,150 points
939 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 by Aditya
• 7,660 points

Related Questions In Others

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 in Others by narikkadan
• 18,480 points
279 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 in Others by narikkadan
• 18,480 points
119 views
0 votes
1 answer

Using unserialize in PHP throws the same error repeatedly

I googled 'Node no longer exists', and ...READ MORE

answered Nov 14, 2018 in Others by DataKing99
• 8,240 points
836 views
0 votes
2 answers

How to get the URL of the current tab in Google Chrome?

Its so simple.... If you want to ...READ MORE

answered Aug 12, 2020 in Others by Steve
• 200 points
940 views
0 votes
1 answer

Simple question regarding PHP, Wordpress & Yoast SEO

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

answered Feb 14 in Others by narikkadan
• 18,480 points
45 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 in PHP by Kichu
• 19,040 points
365 views
0 votes
0 answers

PHP generate file for download then redirect

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

Jun 20 in PHP by narikkadan
• 18,480 points
76 views
0 votes
0 answers

PHP page redirect

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

Jun 23 in PHP by narikkadan
• 18,480 points
57 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 in Others by Aditya
• 7,660 points
48 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 in Others by Aditya
• 7,660 points
459 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP