php headers already sent error [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
I have a single php file, with this code in it:
<?php header("Location: somepage.php"); ?>
Absolutely no spaces before or after the php opening or closing tags and I am getting the
Warning: Cannot modify header information - headers already sent by (output started at C:\ ... \test.php:1
Usually when I get this error, the 1 is the line number of the problem, but in this case line one doesn't have any extra whitespace. I only have this code on the page and I am not including any other files.
I'm using xampp on windows xp. Any ideas what's going on or how I can track down what's really causing the problem.
Thanks

Chances are that you have what's known as a BOM (Byte Order Mark) in the file. Depending on what editor you're using, you'll need to turn off adding the BOM...

Try to copy / paste your content to another new file using Notepad

Try using WAMP, it doesnt show this error, make sure you donot have spaces before or after the code. For that, if you are using notepad ++ or editplus, go to format and select unix formatting and things should work fine.
OR
try placing this at the top of your page:
<? ob_start(); ?>
then at the bottom of the page place this line of code:
<? ob_flush(); ?>

If you are sure it is not a BOM causing the error, check your httpd.conf and any .htaccess files if the prepend any other files. Look out for something like this:
<FilesMatch "\.(html?)$">
php_value auto_prepend_file /server/path/to/my/www_root/subdir/file.ext"
</FilesMatch>
Also check your PHP.ini: http://de.php.net/manual/en/ini.core.php#ini.auto-prepend-file

Related

Warning: Cannot modify header information - headers already sent by php error [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
After I changed something in my .php file my whole site got an error. I changed the file back to the old one but now I get this error at the top of my page:
Warning: Cannot modify header information - headers already sent by
(output started at
/home/test/domains/test.nl/public_html/wp-content/themes/mugen/engine/theme-scripts.php:1)
in
/home/test/domains/test.nl/public_html/wp-content/plugins/woocommerce/classes/class-wc-session-handler.php
on line 63
The plugin is Woocommerce but it was working before with the same code but now I keep getting this error at the top.
Code of the file I changed aka the theme-scripts.php file:
http://pastebin.com/mU4DNLnm
How did you edit the file? I'd guess you now have a Unicode Byte Order Mark at the beginning of it. Can whatever editor you used save the file with the option "without BOM"? Most decent ones can.
The big clue here is that your error comes from line 1, and line one of your file is just:
<?php
...which is an indicator that there may be something "invisible" right at the start of the file.
This error normally means you have already 'print'ed or 'echo'ed some text before php trying to write into the header info.

header has error in php , why? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Cannot send session cookie - headers already sent
Headers already sent by PHP
My previous problem was for UTF-8 but now ! again my code generates this error :
Warning: Cannot modify header information - headers already sent by (output started at F:\xampp\htdocs\1\error_list.php:34) in F:\xampp\htdocs\1\error_list.php on line 75
why?
my code is :
header("location:list.php?msg=Please enter some username and password");
There are several things 'wrong' with your code:
Don't use shorttags. They aren't really wrong but I don't like them :)
I think your define should be: define('ADMIN', $_SESSION['name']);
You problem either comes up because there is already some html or anything (space) outputted to the browser. If you have saved the file as UTF-8 you should make sure it doesn't include a BOM
This happen when you send html code even a single space to the browser. If you have any html code before this php code, so that the problem.
Solution
To tell the browser to buffer everything except php codes, you need to use:
ob_start();
when browser read the the function, it will buffer everything and never send it to the browser until your php code has done its task.
EDIT
Well, If you have some html code before your php code, what do you wanna do? Is there any thing you can do?
This a workaround for the problem, why not???

Warning: Cannot modify header information [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
After 2 hours of searching for answers i have still not fixed this problem.
I get Warning: Cannot modify header information - headers already sent by (output started at /home/xxx/public_html/xxx1.php:6) in /home/xxx/public_html/xxx2.php on line 10
What have i done so far?
-Checked for no whitespace, in xxx1 and xxx2. I have just change webhosting and now the problem apperas. It says it the file xxx1 on line 6. Please can any one sort this out? This is the code.
<?php
require_once(__ROOT__.'/config.php');
?><head>
<script src="scripts/func.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="css/yui.css">
<script src="scripts/yahoo-dom-event.js"></script>
<script src="scripts/element-min.js"></script>
<script src="scripts/container_core-min.js"></script>
<script src="scripts/editor-min.js"></script>
<script src="scripts/func.js" type="text/javascript"></script>
</head>
<?
BOM - byte order mark:
make sure this file and config.php are encoded without BOM.
in notepad++: go to Encoding -> UTF8 without BOM.
Your previous host had output buffering configured, your new host does not. This is the reason you are getting the problem.
Assuming the code above is from xxx1, on line 6 you are starting to output data, because the is something outside the <?php ?> tags, which is treated as data to send straight to the browser. Then, later on, on line 10 of script xxx2, there is a call to header(), setcookie() or some other function that is trying to modify the response headers.
You cannot modify the response headers after you have started sending the body, because the headers have already been sent.
You will have to either:
Store the HTML from above in a string, and output it after line 10 of xxx2 or
Move the call to the function that tries to change the headers to before line 6 of xxx1
Without seeing more code I/we cannot tell you exactly how to do this.
maybe in config.php after closer tag ?> space appear or empty line - for php5 < 5.3 this is a problem. Maybe like this
<?php
//code here
?>
//this may be a space
// or one more new line
That's why zend code style guide advice not close php tag
Example file xxx.php
<?php
//not closer tag in end of file
In both you can use ob_clean() - and this help you clean buffer and invistigate mistake - try to put it in any line.

PHP headers already sent [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 2 years ago.
Possible Duplicate:
Headers already sent by PHP
Getting the following error:
"Warning: Cannot modify header information - headers already sent by (output started at..."
for the following line:
echo '<center>Current Time is '. gmdate("H:i A") . ' GMT (Greenwich Mean Time or UTC)<br />';
If I comment it out it just throws up the error at the next echo statement. Thoughts on why PHP hates my echo statements so much?
Here is my include toward the bottom of the HTML:
<div id="saveCanForm" width="100%">
<?php include('savereport.php'); ?>
</div>
It's not the echo statements that are the problem. It looks like you have a header call somewhere later in the file, but you can't send headers once you output any text at all. You could either move the headers to the beginning of the script or alternatively use output buffering.
Because our echos are coming before you are sending the header which is not allowed. Ensure that header go before any of your output.
If you don't want to rearrange you can also use output buffering.
Headers are dealt with before there is any other output, so if you write something out then PHP can't properly send headers afterwards. At some point in your code you are giving HEAD instructions which hence fails. (There's technical reasons for this, like redirects and so forth)
Its not the echo which is the problem. It is most probably caused by a file that you have included in the .php file. Have you included a file at all? This will probably be at the top. If you included file statement is not at the top of the file make sure it is.

Warning: Cannot modify header information. Can't find error [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I'm using a login-script and on my developmachine it worked perfectly but when I moved it to my webhost it stopped working.
The code can be viewed here: http://beta.yapaste.com/fm
The error I get when I login is
Warning: Cannot modify header information - headers already sent by (output started at /*/*/*/*/*/*/login.php:10) in /*/*/*/*/*/*/inc/login.php on line 43
But I can't find the error.
Thanks for answers, Victor.
Edit: Found the error, I included the file that I've got on yapaste in another file so the php-code wasent first.
On line 10 of login.php something is sent to the browser (white space between ?> and <?PHP tags count), at which point headers must be sent to. This triggers a warning when you try to then add more headers after this, on line 43.
You need to have a look at line 10 and prevent the output being sent, or use output buffering - your previous host could have had this on by default which might explain why it no longer works...
use ob_start(); at entry of your script to start buffering, but as Simon said, propably there is some white space wich is sended to browser befor you try to change headers.
Remeber, that you can omit ending php tag if you don't have any output code after, That will secure you from white spaces at end of file.
Also check for format of your source files - if they are encoded in UTF-8 with BOM, there are some starting characters that could be not shown in editor at beginning of the file.

Categories