Solve "Cannot modify header information - headers already sent by *.php"
Warning: Cannot modify header information - headers already sent by somefile.php (output started at somefile.php:###) in somefile.php on line ###
This Warning is shown by PHP when you use the header function to output headers or use the setcookie function to set cookies after any echo or content which is not inside PHP tag.<?php
echo "Hello World";
header("Location: /redirect.php");
?>
Hello World !!!
<?php header("Location: /redirect.php");?>
Both should throw this warning.
You can disable this warning by turning the error_reporting to OFF using error_reporting(0);
, which is pretty much the not way of doing things. In HTTP Headers are sent before the actual content so if you are going to use header function after outputting the contents, it is likely to not work.
How do I solve this Warning Cannot Modify Header Information ?
In order to solve this issue you have to make sure you dont echo or print any output before using header or setcookie function. Make sure the control passes through these functions and then to the output part.I am sure I dont output anything before calling header or setcookie
Just see all your files for white spaces or empty new lines. For example if you include a file before using the header or setcookie functions, make sure the file does not have a closing php tag (?>) at the end of the file. This is recommended by PHP so that stray white space or new line characters at the end of this included file gets outputted.//<-- A new line on top ! Oops.
<?php header("Location: /redirect.php");?>
// <-- A blank line at the end?
I still want to output first and then use header function
In that case you can use output buffering functions to buffer your output automatically before senting any output and the output is sent as a chunk at the end. The following code snippet shows this :<?php
ob_start(); ?>
Hello World !!!
<?php
setcookie("name","value",100);
?>
Again !!!
<?php
ob_end_flush();
?>
But my code is already done and I dont want to add ob_* functions to it !
You can turn on buffering in PHP on by default using php.ini. Look for output_buffering , The following shows the mentioned part of the php.ini file. Set the value as On and restart Apache server.; Output buffering allows you to send header lines (including cookies) even
; after you send body content, at the price of slowing PHP's output layer a
; bit. You can enable output buffering during runtime by calling the output
; buffering functions. You can also enable output buffering for all files by
; setting this directive to On. If you wish to limit the size of the buffer
; to a certain size - you can use a maximum number of bytes instead of 'On', as
; a value for this directive (e.g., output_buffering=4096).
output_buffering = On
Video to fix your warning cannot modify header information
Warning Cannot Modify Header Information :) 17 Mar, 2010
You know what would be a GREAT idea ?
Simply to post a sample code using headers that works. I went through all these convoluted posts and am not better. Actually quite confused. This page is a list of code that does not work, and I still do not understand where ob_start should be placed ...
Sorry to be blunt, but teaching by example is often much better than cryptic dialogs and answers. Will appreciate a written example code, indeed. Thank you in advance.
For me the eror was consistenr until I removed BOM formatting. You can easy doit with Notepad++ on Encoding menu choose encode Utf8 without BOM and save
Good luck
<?php ob_start(); $hearingform = ' <html> <head> <style type="text/css"> ..... </style> <div align="center" id="FreezePane" class="FreezePaneOff"> <div id="InnerFreezePane" class="InnerFreezePane"> </div> </div> <script type="text/javascript"> function FreezeScreen(msg) { scroll(0,0); var outerPane = document.getElementById("FreezePane"); var innerPane = document.getElementById("InnerFreezePane"); if (outerPane) outerPane.className = "FreezePaneOn"; if (innerPane) innerPane.innerHTML = msg; } function unFreezeScreen() { var outerPane = document.getElementById("FreezePane"); outerPane.className = "FreezePaneOff"; }'; // some code to pull data from the database... echo '<script type="text/javascript">unFreezeScreen();</script>'; /*send data to an excel file*/ header("Content-type: application/csv"); header("Content-Disposition: attachment; filename=$filename"); echo $data; } //END post IF ob_flush(); ?>
ob_start does not affect javascript
ob_start and ob_flush is most likely to not cause any issues on javascript, please check console for any likely javascript errors.
I think you have missed an ending script tag after the unFreezeScreen definition.
Hi digitalpbk,
Thanks for your response. I think I did have the ending tag "}" if that is what you meant.
function unFreezeScreen() {
var outerPane = document.getElementById("FreezePane");
outerPane.className = "FreezePaneOff";
}
The js script above didn't run after I added ob_start(). If I take off the ob_start then it runs but I also get the header warning errors.
Script ending missing
I meant </script> tag.
Thank you! One of the plugins uses setcookie, and I put ob_start / ob_end_flush to no avail. I fixed my php.ini for output buffering and finally it does the job!
<?php ob_start(); ?> <?php ob_end_flush(); ?>
101% works for me . thank you sir !<?php ob_start(); abstract class Controller { protected $registry; protected $id; protected $layout; protected $template; protected $children = array(); protected $data = array(); protected $output; public function __construct($registry) { $this->registry = $registry; } public function __get($key) { return $this->registry->get($key); } public function __set($key, $value) { $this->registry->set($key, $value); } protected function forward($route, $args = array()) { return new Action($route, $args); } protected function redirect($url, $status = 302) { header('Status: ' . $status); header('Location: ' . str_replace('&', '&', $url)); exit(); } protected function getChild($child, $args = array()) { $action = new Action($child, $args); $file = $action->getFile(); $class = $action->getClass(); $method = $action->getMethod(); if (file_exists($file)) { require_once($file); $controller = new $class($this->registry); $controller->$method($args); return $controller->output; } else { trigger_error('Error: Could not load controller ' . $child . '!'); exit(); } } protected function render() { foreach ($this->children as $child) { $this->data[basename($child)] = $this->getChild($child); } if (file_exists(DIR_TEMPLATE . $this->template)) { extract($this->data); ob_start(); require(DIR_TEMPLATE . $this->template); $this->output = ob_get_contents(); ob_end_clean(); return $this->output; } else { trigger_error('Error: Could not load template ' . DIR_TEMPLATE . $this->template . '!'); exit(); } } } ob_end_flush();?>
any suggestion plez...Check index.php for output statements
Please check the file at /www/zymichost.com/n/i/k/nikstore/htdocs/install/index.php:2
And add ob_start() ob_end_flush() to the index.php
My problem was solved when i used put all php code in a single php tag
<?php
my code
?>
it means when we use
<?php
some code
?>
any html coding
<?php
some code
?>
then it was giving error
but i have copied all code in single php tag and really my problem was solved
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <div id="fb-root"></div> <script type="text/javascript"> window.fbAsyncInit = function() { FB.init({appId: '<?php echo $app_id;?>', status: true, cookie: true, xfbml: true});
and as for the index.php that is the file that starts the whole thing it starts like this:<?php require_once 'config.php'; require_once 'facebook.php'; //////end constatnt $facebook = new Facebook(array( 'appId' => $app_id, 'secret' => $fb_app_secret, 'cookie' => true,
Any Suggestions? Thank you very much in advance.<?php ob_start(); mysql_connect('localhost','root',''); mysql_select_db('rajashiv_wrdp1'); $insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$_POST[name]','$_POST[comment]') "); if($insert) { header('Location:http:edit.php'); }ob_flush(); ?>
this error can occur Cannot modify header information - headers already sent by (output started at /home/rajashiv/public_html/data.php:2) in /home/rajashiv/public_html/data.php on line 9Space before <?php
Is there a space before <?php tag?
php to stream mp3s
I was using a code to substitute the url adress with an id to stream mp3s. Adding ob_ functions fixed the problem for me.
Thank you for the solution by the way.
ob_start worked
Great Guy!!! <?php ob_start(); ?> did the job.
The Use of @
I tried to use @header("$location:mylocation"). It just works.
Use of @
@ just suppresses any warnings on that line. This method is not recommended.Problem with "own wiki an installing extensions"!!!!!!!!!!!!!!!!
Warning: Cannot modify header information - headers already sent by (output started at /users/knowpedia/www/Knowpedia/LocalSettings.php:1) in /users/knowpedia/www/Knowpedia/includes/WebResponse.php on line 22
what can i do, to make changes with extensions effect? hope everyone can help me.
regards
Thank You! This was driving me nuts!
Putting this at the top of the file:
<? ob_start(); ?>
and this at the bottom
<? ob_flush(); ?>
Did the trick for me! The project I'm working on was a WordPress install as well. Was doing some custom stuff and this post helped me fix it! Thanks again.
Warning: Cannot modify header information - headers already sent
i got the same problem
Warning: Cannot modify header information - headers already sent by (output started at /......./header.php:112) in /xxxxx.php on line 24
but header file line 112 is below code
<style type="text/css">
why is that..??
im tired to sloved this problem..please help me
display errors off in php.ini
but still its gave the same error
i cant fix..
please help me'
thanks
What is on line 24?
It says the output started on line 24, verify the line, or else try any of the methods above. ob_start(); ob_funcs()You are awesome!
Thanks a ton man! One of my client's came up to me with this problem in one of his old code! Had to go over the whole CMS code looking for that blank space hidden somewhere haha.. But in the end, your php.ini edit worked like a charm! :) You are awesome! thanks.
header:location problem
here is the code....this is showing warning:header already sent..i have used ob_start() and ob_end_flush() in this code..still it's giving the warning..
<?php ob_start(); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<style type="text/css">
<!--
.style1 {
font-size: 24px;
font-weight: bold;
}
.style2 {font-size: 18px}
.style3 {color: #FFFFFF}
-->
</style>
</head>
<body>
<?php include("cdb.php");?>
<?php
if(isset($_POST['Submit']))
{if(!$_POST['name']||!$_POST['pwd'])
die("<br><br><br>you did not fill in a required field...");
$check=mysql_query("select * from adminlog where admin_name='".$_POST['name']."'");
$check2=mysql_num_rows($check);
if($check2==0)
die("<br><br>that user does not exist in ur database");
while($info=mysql_fetch_array($check))
{
if($_POST['pwd']!=$info['admin_pwd'])
die("Incorrect password,please try again");
else
{$_POST['name']==Stripslashes($_POST['name']);
$hour=time()+3600;
setcookie(ID_my_site,$_POST['name'],$hour);
setcookie(Key_my_site,$_POST['pwd'],$hour);
header("Location:status.php");}}} ?>
<?php ob_end_flush(); ?>
</body>
</html>
plz help me to solve this problem...:( :(
thnx in advance sir..
Spaces before ob_start ?
Is the code complete ? Make sure you dont have any spaces beforethnx a lot..:) now the same
thnx a lot..:) now the same code is running...
redirect user in php : no header already sent problem
if u too got fade up with the error in php page...
"headers already sent output started by ........."
and want to redirect your user to new page try this simple script
hmmn after searching a lot simply use in php
echo "<script>location.href='service.php';</script>";
you can place it anywhere (in condition statement, in body, after html tags ) no header already sent problem ...
Header redirection using script
This does not work unless the echo is enclosed in a script tag. This is front end browser redirection example using javascript, so not the best method, since there is no fallback when javascript is not present.Header Problem Solved
Thank digitalpbk for grateful suggestion.
I solved "Cannot modify header information - headers already sent by" problem by using ob_start() and ob_end_flush() code.
example:
<?php
ob_start();
// my code...........................
ob_end_flush();
?>
Header
Hi everyone,
I'm experiencing similar challenge, the line its making reference to is like this:
header("Content-type:text/html;charset=utf-8");
if (Navigator::isRequestedUnderLegalURI()){
$uri = Navigator::getUri();
//$page_config = new PageConfig($uri);
$page_config = PageConfig::getPageConfig ($uri);
// the Responder!!!
if ($page_config->PageExists()){
$page_content = System::getPage($page_config);
echo $page_content;
please what do I change here?
Thanks
Hello
It is very good blog.I am also looking for this from a long time.Many people like to visit here.I have seen many things over here.It is very good.
Good Post...
Good Post...
can't undersatnd how can do
can't undersatnd how can do it??
What part do you need help with ?
What part do you need help with ?Thank you!!!! This was
Thank you!!!! This was extremely helpful! Amazing what one space can do...
Thanks!!
thanks alot, edit php.ini file solve my problem on my client site.
Its good idea
Thanks for the help it will work for me.
A good idea is to add gzip compression.
cannot modify header help
thanks for the info,
i have the same situation but it didnt worked in me.
i am hosting my site in zymic.com
and i have this problem
output started at /www/clanteam.com/k/e/l/kel-inventory/htdocs/login.php:1) in /www/clanteam.com/k/e/l/kel-inventory/htdocs/login.php on line 56
Warning: Cannot modify header information - headers already sent by (output started at /www/clanteam.com/k/e/l/kel-inventory/htdocs/login.php:1) in /www/clanteam.com/k/e/l/kel-inventory/htdocs/login.php on line 57
i already put the ob_start and the ob_flush, i followed your instructions but nothings happen, still got the same problem.
can you please help me?
im a newbie here. so thanks a lot .. :)
Where have you put the ob_* ?
Can you tell me where you have put the functions ?same problem
i have the same problem, but anything of your tips resolved my problem :( what i can do, all in seo pack caused problem in my header.
Thnak You
i have visited almost all blogs , but you are the great man who give me the proper solution . you save my life by ob_start(); .
Thank you very much .
Thanks for the explanation and additional information
Had the same problem, but as publishing in a CMS I could not turn on output_buffering. The next Problem was the inclusion of php-Files: They were strangly included by a html-Tag npsobj so I couldn't force the header-modification to be made before html-Tags were included. I also couldn't modify the code to have ob_start(); before outputting (which also would have left my php-Code untouched...) So: I was reeeally greatful to understand the hole header-modification issue to work out the above mentioned problems:
Insert the path of your php-file to a the action-Attribute of a form: When clicking the submit-button a new page is enforced starting without output
--> No Problems!
Thank you so much
to all of the blog and forums I'd visited, your blog solved my problem.
ob_start redirect
Quality solution, was having trouble with conditional redirects as old developer used meta refreshes, this trick solved my issues
many thanks
your my hero :D
thanks alot, edit php.ini file solve my problem on my client site..
ob_start();
Awesome! I included ob_start(); in every php file that had errors and it did the job~!
Thank you.
Good work.
ob_start(); fixed my problem.