Cannot modify header information (connot detect header) - php

I checked all answers in different pages and use it, but the error still appears. Please help me.
The error is:
Warning: Cannot modify header information - headers already sent by (output started at /home/iraustor/public_html/copytest/post.php:1) in /home/iraustor/public_html/copytest/post.php on line 57
The URL of form is here: http://iraust.org/copytest/contact.html
And the page that after complete the form is: http://www.iraust.org/copytest/thanks.html (or any other method to shod this message)
It has taken 2 days but answer. Please help me.
"header("Location:$Redirect_Page");"

If you issue headers, like you do for a redirect (setting Location) you MUST be sure that there's no other output before that statement, as PHP will already build the headers (however maybe not yet flushing them to the client) on the first output.
This might be the case for several reasons (unexpected error in some require, a whitespace at the beginning or end of some file, etc, but the error message you have is clear in mentioning where this output started: /home/iraustor/public_html/copytest/post.php:1.
You should double check that there's nothing before the opening and after the closing <?php ... ?> block. This applies to all included or required files in called script.

As pointed out by h7r, if you use the header function you cannot print anything before its call.
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
From Header documentation on PHP.net
So, the first thing you should check is the line 57 in your post.php file: there starts the output that causes the error.
Be sure that no output is sent: also a white space or a blank lines is an output and this cause the error.
If you like, you can use the output control functions to buffer the output: in this way you can print what you want but all your outputs aren't sent immediately to browser, so you can use the header function without causing errors.

Put your code somewhere for us to look at...
Possibly PHP could be outputting an error, or a warning, etc... It might not be you for example.

no space before
I completely confused!
The form is working and the information will be send to e-mail
but the error makes feeling nervous for users

The problem solved by simple code editor (notpad++)
problem: hidden white space and non Unicode characters

Related

Where is the data coming from?

It seems I met this problem before, in a different disguise. When a file was being "included" in my main php file before starting a session in that same main php file, I'd get the "headers already sent" error because my included php had a blank line at the start, which was being somehow sent before anything should be sent. Delete blank line, "problem solved"...
Now I have this ajax thingy where I scan a certain string returned by a php. The string I hardcoded in this php to be returned to the ajax callback was the string "alert", which was never seen. I scan what is being received by the callback, and guess what? It has a character 10 starting my 5 character "alert" string.
Sounds familiar? yes, I had an include before the "echo("alert");", and that include'd file DID start with a blank line (too! why I keep doing this???). Delete line, and now I don't get the character 10 (ascii "new line" eh?) starting my "alert" string anymore.
The question: why is the php echo'ing a "new line" character that was never formally "echoed" along with my carefully crafted string? Is this a bug of mine or php? Thanks in advance.
Everything not inside your <?php ... ?> tags is treated as text and sent to the client. So make sure not to have empty lines in your source files...
Edit: The purpose of this is to have a simple way to have a HTML file mixed with portions of PHP code without the need to echo all the HTML stuff explicitly...

PHPExcel outputs garbled text

Like many others out there i have had my fair share of issues trying to download an Excel file output by PHPExcel.
What happened in my case was whenever I wanted to download a file using
$obj->save('php://output')
i always used to get garbled text in my excel file with a warning saying my file was corrupt. Eventually i resolved the issue. The problem being i had a
require('dbcon.php')
at the top of my php script. I just replaced that with whatever was there inside dbcon.php and it worked fine again.
Though the problem is solved i would really like to know what caused the problem. It would be great if anyone out there could help me out with this one.
Thanks.
If you get that error - you should follow the advice we always give in that situation: you use a text editor to look in the generated file for leading or trailing whitespace, or plaintext error messages - and then in your own scripts for anything that might generate that such as echo statements, blank lines outside ?> <?php, etc.
Another way of testing for this is to save to the filesystem rather than php://output and see if you get the same problem: if that works, then the problem is always something that your own script is sending to php://output as well.
Clearly you had a problem along those lines in your dbcon.php file. This can be as simple as a trailing newline after a closing ?> in the file...
Tanmay.
In situations like your's, there can be couple of reasons for broken output:
in file dbcon.php can be a whitespace before opening or ending php
tag, so that produce some chars to output and can make file broken
(this is reason for using only opening tag in php 5.3+);
maybe file dbcon.php wasn't found by require, so you got error message in otput;
any other errors or notices or warnings in dbcon.php, because presence of global vars from current file..

session_regenerate_id and setcookie on same page in PHP

Ok, so I am working on this login system and of course when the user logs in i regenerate the session id.
But after I regenerate my session id i also want to set a token to be stored in a cookie. However I seem to not be able to do that on the same page. I get an error that says:
Warning: session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent in /htdocs/somesite/test.php on line 44
Warning: Cannot modify header information - headers already sent by (output started at //htdocs/somesite/test.php:76) in /htdocs/somesite/test.php on line 54
This what I am doing right now:
session_regenerate_id();
setcookie("Foo","Bar", time()+$CookieExpireTime,"/");
I am wondering if there's a work around this. It's working when I'm testing it on localhost, but not on the server...
Common Cause:
This warning is often caused by a blank space or extra line at the beginning or end of a .php file.
Check the error for the filename that generated the error (ie: the "output started at...." filename), open that file in your text editor and remove the extra spaces or lines immediately before the first
Other Causes: Syntax errors
In the example above, you'll see output started at /....includes/something/something/something.php:12. The includes/something/something/something.php is the filename you need to be concerned about. The :12 means that the problem you need to fix is on line 12.
In that same example, the "in /includes/something_else.php on line 67" message can be completely ignored. It is not the problem. It is the one that discovered that the problem had already occurred.
If the "headers already sent" error appears AFTER any other error, then you need to fix that other error FIRST. (The error message itself is what caused headers to be sent, so fixing that error will cause the second error to go away too.)
If the "started at" refers to line 1, then it's either a space before the opening <?php tag or it's incorrect encoding on the file.
Remember, if your language is in UTF8, then you MUST encode the file as "UTF8-without-BOM", else the BOM (byte-order-mark, an invisible character at the beginning of the file) will cause this same headers-already-sent error.
Summary:
a) look for where it 'started at'
b) track the line number
c) check what's normally happening on that line.
If it's the end of the file, then it's blank spaces.
If it's the start of the file, it's likely spaces or incorrect encoding.
Elsewhere it could be a syntax error or the result of an "echo()" statement which is displaying info or perhaps debug code.
Common syntax errors include the use of single-quotes inside statements that already have single-quotes. Check to be sure your quotes aren't mismatched. If you need to use single-quotes while inside other single-quotes, change yours to \' instead of just '.
d) the rest of the info simply shows other execution information, mainly the part of the code that discovered that it cannot proceed as expected due to the problem that happened in the 'started at' location.
Source: zen-cart.com
Another similar thread on SO: How to fix "Headers already sent" error in PHP
I hope this helps.
The problem isn't with the two functions, the problem is that you cannot call session_regenerate_id() once any output has been sent to the page. That function, like header() has to be called before any sort of output is sent to the page.
Either re-structure your page so that this function is called before any output is sent, or use output buffering so that this function is called before any page content is sent.

Sometimes redirecting failed mysteriously in PHP?

#header('Location: ' . $uri);
But it only seldom happens,what's the problem?
Don't use # to supress the error and check your logs, it is one of a few things:
Previous error output stopping the redirect?
$uri is bad sometimes
Something else in your code is bad.
Debugging is the answer.
Perhaps your script is sending some output before it for some reason. Remove the #.
Well, it is most likely because the headers were already sent due to whitespace. You'll never know this because you have a # sign in front of it that stops errors from being displayed.
Generally this whitespace might be caused by another error, or it might only happen when you include a certain file.
The other possibility is that $url is not properly formatted, or somehow the header just looks wrong to the browser. In which case the browser might choose to ignore it. Echo out the complete Header you are sending and make sure it looks right.
Finally, some browsers will stop redirecting if you have redirected too many times in a row.
The header might not be sent right away. Always use die() after header().

PHP warning: headers already sent in Unknown [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I'm looking for things that might trigger the following PHP warning:
PHP Warning: Cannot modify header
information - headers already sent in
Unknown on line 0
Turned out that it was the line
ob_start("ob_gzhandler");
that caused the warning. This has been reported and fixed in 2001, it seems, but for some reason it keeps coming back.
It might be a lot of things, but as the others said, it's often just a space lying around somewhere that gets outputted and then a header() command is sent which normally is fine, but not after starting to send content back (potentially just a space in this case).
Using ob_start() stops the output from going out right away by buffering it. So it's a potential solution, or at least way to diagnose where it's coming from as zodeus said.
One common thing that causes those lose spaces are in this scenario.
global.php
<?php
$variable = 1;
$database = 'something else';
?> <-- A space here
<-- Or here
index.php
<?php
require('global.php');
$var = dosomething();
header('Location: http://www.example.com/');
?>
One way to fix that is to remove the ?> at the end of the global.php file. You don't need those, they are only useful if you start putting HTML for example after your PHP code. So you'd have:
<?php
$variable = 1;
$database = 'something else';
And when you do the require(), the space is not outputted before the header().
Just to illustrate the problems with content outputted and headers is that other common case that gives a similar error. It happens when you forget to stop the processing after a redirection using header().
if ($notLoggedIn) {
header('Location: http://www.example.com/login.php');
}
echo 'Welcome to my website'; // this will be outputted,
// you should have an exit()
// right after the header()
I think whats happening is one of the built in php functions is outputting something. I've seen this with a couple of the IMAP functions where they out put just a " " (space character) and it screws things up.
You can thry tracking it down using Xdebug or the Zend debugger, but i f you have neither
try wrapping output buffering around some of the functions you think may be cause it.
ob_start();
callYourFunction();
ob_end_clean();
Do this one function at a time and when the error goes away you'll know which function is cause you the problem, then you can either file a bug report or just leave it in as a hack. But at least then you know what function is cause the issue.
Edit: The fact that is says your output is occurring on line 0 means that it's a C level function doing the output, not any code that's been written using PHP.
Have you checked your files for unintended UTF-8 BOMs?
The error tells you that something has sent output, which would force headers to be sent, because the headers must be written before the body of the http message.
The most common problem I have found is text in headers. vis:
<?php // myfile.php
include 'header.php';
?>
and in header.php:
<?php // header.php
....
?>
What you can't see here is that there is a blank - either a space or CR/LF after the closing '?>'. This is output because the php standard says that anything outside the php tags is output as html.
The solution is to make sure that you make sure to erase everything after the closing '?>'

Categories