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
• 8,730 points

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,280 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
• 7,860 points
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
• 7,860 points
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
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
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
• 7,860 points
+1 vote
2 answers

Scp Php files into server using gradle

Tru something like this: plugins { id ...READ MORE

answered Oct 11, 2018 in DevOps & Agile by lina
• 8,220 points
0 votes
1 answer

How do I create folder under an Amazon S3 bucket through PHP API?

Of Course, it is possible to create ...READ MORE

answered Apr 24, 2018 in AWS by anonymous
0 votes
1 answer

Failure uploading Image on AmazonS3 with PHP SDK

Try this, I took it out from ...READ MORE

answered May 4, 2018 in AWS by Cloud gunner
• 4,650 points
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,280 points
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,280 points
Send OTP
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP