Going through the php.net site, it had an example for header, which says would give me error. I copied it, and executed on on WAMP, but it didn't showed me any error, but did redirect to the site.
<html>
<?php
/* This should give an error (but it doesn't!). Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
?>
Just wanted to know, if its a right behavior on my WAMP, or its an error, or if I have any particular settings active in php.ini file which is making this work!!!. Let me know if anyone needs my php.ini to be copied here!!
Thanks,
Tanmay
It sounds like you have output_buffering enabled.
http://php.net/manual/en/outcontrol.configuration.php
Standard configuration would be to error because data has already been output, and headers need to come first. Output buffering would allow headers to appear in code after other output, but it would still output the headers first due to the buffer.
Headers are sent as soon as any text is sent to the browser, and can only be sent once. once the is sent, headers are sent along with it, so trying the header function after that wll throw a headers already sent error.
Related
I moved my documents to a new host and the headers stopped working(refresh,redirect,etc).They used to work in my old server.I have googled and tried adding a ob_start before sending headers, that did not work.
Here is a part of the code...
if(isset($_GET['reflink']))
{
echo '<h3>Already Logged In<h3><p>Please logout before registering an account.Redirecting you back to where you came from...';
header('Refresh: 3; url="http://www.xacnr.com'.$_GET['reflink'].'"');
}
*It used to work before, it must be a problem with the server settings or something :|
A short answer is:
It is not possible to send http headers after the output of anything else.
So if you want to output headers, you must do this at the beginning of your output. To be even more precise: HTTP-Headers must be the first thing of your output, if you have to send them - before anything else.
Please read the documentation:
http://www.php.net/manual/en/function.header.php
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
Please be also aware of the fact that the Refresh-Header is AFAIK not part of the official HTTP-Standard. It's a jurassic artifact from Netscape which will be still accepted and interpreted by most browsers, but this may change even without special notice.
If you need such a refresh and if you want to stay on the safe side, you should consider using the Meta-Refresh within the HTML-Header.
Please read here:
http://en.wikipedia.org/wiki/Meta_refresh
BTW: It's also a bad idea to use unsanitized, unprocessed values from $_GET, $_POST etc. Your example should never be used in any public available environment.
You do an output (echo) before you send the header:
echo '<h3>Already Logged In<h3><p>Please logout before registering an account.Redirecting you back to where you came from...';
header('Refresh: 3; url="http://www.xacnr.com'.$_GET['reflink'].'"');
The simple but strict rule is:
No output before the header!
From the Docs:
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.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
I got this warning in php:
Cannot modify header information - headers already sent by (output started at
When I installed mysql, apache, and php separately but when I'm using Wamp the warning does not exist and the page redirects gracefully.
I have already tried setting the error reporting to only display errors and not warnings and what happened is that the warning didn't show up but the page also didn't redirect.
I know this is already considered bad practice but my code base has a lot of these codes and modifying them all might introduce new bugs to the code.
Is there any way to completely ignore this warning in php?
The classic way to avoid - and not ignore - this problem is with output buffering, ob_start() and ob_flush(). Start the buffering before any output, and finish with the flush explicitly, or it may do so automatically. This captures any output rather than printing it, and so any headers that are output are done so before any visible content.
Fixing the bug of things being output, from the filename you had in the error message is the best way to fix this though. Removing the closing '?>' from files when it's not needed might help.
This is an horrible practice but just set
error_reporting(0);
at the beginning of your script. Otherwise setit in php.ini
display_errors = Off
EDIT - probably you are outputting something before the redirect. I once spent three days debugging this and the reason sometimes is whitespace at the end of files or the fact that you output some HTML to the browser before the redirect.
I usually never close the php tag to avoid this
Even when you don't show your error your application will not work. This is because like the error said the headers are already sent. What this means is the following:
A browser requests your php file at the server. The server then executes this and sends back the resulting html. This error tells you that you are sending something back to the browser after you already sent the result of your php file. So I'd suggest you try to fix the error your getting instead of just not showing it.
You can't "ignore" this warning and redirect. The warning says so clearly:
Can't send headers! Headers already send by output!
This means that the header (the Location header cannot be sent because output was already sent. (Headers may only be sent before any and all output).
Please RTM, you need to fix your bugs, not ignore them.
The error is caused by you sending some sort of output before the header() function is called, the reason for that may be a lot of things, when unexpected, the immediate suspect is a whitespace at the beginning or ending of some file.
<?php //Note the space before the opening tag!
header("Location: this-will-fail.html");
?>
Please note that this error specifies exactly where in your code you've started to output data to the browser (see the output started at ...1, look for trailing white space \r, \n, tabs & spaces).
In theory, turning off the error reporting will not cause the page to redirect because the headers were already sent and therefore cannot be changed or added to.
What you should do if you need to redirect with data already sent is something like this:
if ( headers_sent() ) // Check if the headers were already sent
{
echo( "<script>document.location.href='$url';</script>" ); // Use client side redirect
die(); // Stop here
}
else
{
header( "Location: $url" ); // Use server side redirect
}
But the best thing would be to find out where the output started and fixing that (if possible).
<?
echo "lalala";
header("Location: http://www.google.com/");
If i put this in a plain php file and deliver over a standard apache2 server with mod-php (PHP Version 5.3.2-1ubuntu4.10) the redirect to google works.
<?
echo "lalala";
flush();
header("Location: http://www.google.com/");
this code does obviously not produce a working redirect.
My question is how the first code is beeing processed and why it works. Because I remember times when things like this were not possible. Is mod-php or apache intelligent enough to buffer the whole request and arrange headers before content?
And:
Can I rely on this if I make sure I don't flush the output manually? Because it would make my application much easier...
Output buffering is probably enabled by default. You should enable it manually if you want to rely on this functionality.
http://php.net/manual/en/function.ob-start.php
The header function ADDS an http common header to the HTTP response. So, the redirect is setted and the browser gets the 302 message before showing you the output.
flush orders php to send the http response already prepared at the point it is called. That's why the second code won't set the header (it must be setted before sending ANY output).
And, the PHP should not output a single thing until:
The script is processed (even if an error stops the parsing)
you set it to send the output somewhere in the script with flush()
Finally, check this on output control http://www.php.net/manual/en/intro.outcontrol.php
I'm using set_cookie() on a site. After adding some functionality, I'm getting Warning: Cannot modify header information - headers already sent by... error. The line number it references as to where the headers initiated from is the very line where set_cookie() is! And I checked, it's not being called twice.
How can I track down these premature headers? I looked at the source code and didn't see any stray characters or anything before the error message starts ( I'm using xdebug, so the first thing is a , which I thought was me, but is actually the beginning of the xdebug message ). I've grepped my code for extra echo and so forth -- nothing.
Can PHP tell me when and where the headers are starting? Or are they really starting on the set_cookie line, and if so, how have I gotten myself into this situation, and how do I get out?
edit: I'm not sure I can paste the code -- do you want just the set_cookie() line? I thought the headers were coming out before that. And there's a lot going on before that with different classes being instantiated. And also I don't think I can post all the classes and stuff -- you know, proprietary information ;)
Edit: I've gotten rid of all terminating delimiters ( all ?>s) and made sure that the first characters of every included file is <?. This is frustrating! It was working before I did these recent changes :(((
Also, why doesn't headers_sent() work?
if ( headers_sent($filename, $linenum) ) {
echo "headers sent: $filename:$linenum.";
}
set_cookie(...);
gives
headers sent: :0.
You can use headers_sent():
if (!headers_sent($filename, $linenum)) {
set_cookie(/*...*/);
} else {
echo "Headers already sent in $filename on line $linenum\n";
exit;
}
One approach would be to run your script from the command line with STDERR redirected to null.
This will show you everything that is being sent to STDOUT, i.e. everything that the server would send to a client.
Hopefully, once you see this, it will be clear where your headers are coming from.
Besides using headers_sent to detect if the HTTP header has already been sent, you could use the PHP’s output control to buffer the output. That allows you to change the header even if you’ve already done some output.
Here’s some example using the explicit output buffering by calling ob_start:
ob_start();
echo 'foobar';
header('Content-Type: text/plain;charset=utf-8');
But make sure you call ob_start before any output was done. Otherwise the HTTP header probably has already been sent.
Can you paste the code?
If its saying headers already sent by [line where set_cookie is], then the error is further on in the script.
Are you sure that you don't have any include files being called prior to your set_cookie() initialization? Do you have sessions running, because if you do, you could just set the cookie at that same point in your script as you started the session.
If there is even a single white space before your set_cookie() initialization it will cause the headers to not be sent correctly. Maybe try using a different editor and checking your results.
I found it! It was a flush(); that I had left lying around all by itself in one of the pages. Surprisingly, it doesn't correctly bind $filename and $linenumber to headers_sent().
I have the following:
$imageurl = "<img class='item_thumb'
src='getimagethumbnail.php?imagename=".urlencode($product_image)."&from=".$prodimagedir."'
min-width='150' min-height='150' border='0' class='item_thumb'>";
Which creates the following html:
<img class="item_thumb" border="0" min-height="150" min-width="150"
src="getimagethumbnail.php?imagename=productsmall_1248886833bloggingbok.jpg&
from=products/"/>
However, the image does not show up. I point my browser to that src link and it gives me a bunch of unreadable text which I am assuming is the image meaning that the script getimagethumbnail is working fine. (I am guessing).
But as I said, the image is not appearing at all. What is wrong and what steps can I take to determine the problem?
Just to add, when I point my browser to that src link: It also gives me:
Warning: Cannot modify header information - headers already sent by
(output started at /home/dji/public_html/getimagethumbnail.php:35) in
/home/dji/public_html/includes/functions.php on line 4953
I am assume this is because of the output?? This script was working fine and I have made no changes to it as far as I am aware!
Thanks
You are trying to send the header('Content-Type') command, after outputting whitespace/characters.
You need to make sure that the header command is before anything is printed on the page.
This will work:
header('Content-Type: ....');
readfile('file.png');
This won't
readfile('file.png');
header('Content-Type: ....');
This is because the header command tells the browser what to look for in the content. All of the headers must be sent before any content because that is how the connections works. The browser can't be told what to expect after the content has already been sent.
Open Connection With Server -> Get Headers -> Get Content -> Close Connection
One of the big reasons behind this is encoding. As the content comes through, the browser has to decode it properly. If you send a header in the middle of the page telling the browser that the encoding type is a, when it was processing it like b, things can get really confusing.
So, in order to send the headers properly, you must put the header command before any output.
That error is caused when you print stuff to the output and then attempt to use the header() method. You shouldn't be outputting anything until after you do what you need with header(). Nothing should precede this, not even white-space.
You already have produced some output (on line 35) before setting the header for the image type. This might simply be a whitespace between php tags, or something you forgot to remove.
Your getimagethumbnail.php script is not generating a valid image; it's including text in it (the warning message you quote), which prevents browsers from rendering it. Judging by the error text, I'd guess this is due to changes made either to getimagethumbnail.php or functions.php.
Fundamentally, the problem is that functions.php is attempting to call header() after output has already been sent to the browser, which just plain won't work. You need to check both files and make sure that any calls to header() come before anything else that sends data to the browser.
You may want to turn off the display_errors setting, as any code which generates any warning or error for any reason will cause the problem you're seeing if the warning/error occurs before your header() calls. (Just make sure you have error logging on, so you can still see what's going wrong!)