Closers & dupers, please not that I am asking for a solution which involves configuring Xdebug - no one has offered one of those yet.
Normally, I debug my PHP in Eclipse, but I also allow adding &debug to a URL to do some simple echo and var_dump() to a browser page (for development only).
I know, I know ... debug by printf() ... shudder!
But, it's quick and it works when needed.
My problem is that the generated page is full of
Warning: Cannot modify header information - headers already sent by (output started at ...
with a backtrace.
Is there any way that I can suppress this?
It looks like you are trying to start a session, do a header redirect, set a cookie, etc. after you have outputted your debug information to the browser. And that leads to the warnings you are receiving.
What you could do, is to start output buffering (perhaps conditionally, depending on your debug variable...) at the very top of your script and dump the output at the end of the script or at least after the sections that cause the warnings.
Related
I have written the below script (snipped) that includes the WordPress functions, however it appears one of the plugins is trying to start a session when it has already been started:
<?php
define('WP_USE_THEMES', false);
require('../../../wp-blog-header.php');
...
?>
I get the following in my php errors log:
<b>Warning</b>: session_start(): Cannot send session cookie - headers already sent in <b>/home/sp32/public_html/wp-content/plugins/woocommerce-abandon-cart-pro/woocommerce-ac.php</b> on line <b>44</b><br />
<br />
<b>Warning</b>: session_start(): Cannot send session cache limiter - headers already sent in <b>/home/sp32/public_html/wp-content/plugins/woocommerce-abandon-cart-pro/woocommerce-ac.php</b> on line <b>44</b><br />
I'm assuming this is an error in the plugin itself, however I want to prevent these warnings from showing in my log file for my specific script. How would I go about that? Ideally I don't want to hide all warnings, just this one.
Note that is not a duplicate- I am not asking how to fix the issue (because the issue lies in a 3rd party plugin), I am asking how to suppress the warning messages.
There are three options I see here:
Fix the offending code. As you have mentioned, this may not be optimal with 3rd party code.
Ask for a refund, and replace this plugin with something well written; specifically, something that abides by an accepted coding standard such as PSR-2, or pear.
Find where the offending files are included (or offending functions are called and wrap them in calls to change the error reporting level (code below). The downside to this is that your sessions will not work, since the headers have already been sent by the plugin; this prevents a session cookie from being set, making the browser unrecognized by php sessions.
$previousErrorLevel = error_reporting();
error_reporting(\E_ERROR);
//offending code inclusion or call here
error_reporting($previousErrorLevel);
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 am trying to fix why my Header command won't redirect back to my main page. It instead just stays on the Php display page.
Here is the php side after I submit my form.
Php Script http://img35.imageshack.us/img35/6602/a9j3.png
Here is the page that I get when my php is put into effect.
Php display http://img40.imageshack.us/img40/6649/1qk3.png
Here is my page it's suppose to redirect to...
Html Main Page http://img801.imageshack.us/img801/8004/tulo.png
As you can see, I have no spacers in my header command for php. I've looked up multiple issues but never found anything that works.
See the line that says:
echo 'Full connection<br/>';
That line will ensure that no headers will be able to be sent after it. You can't output anything to the browser before a header call, including whitespace, HTML or this line.
I'm surprised you aren't getting an error when this happens, it's probably because your error reporting levels are turned down. It's often a good idea to have error reporting turned up high on your local machine when testing so you'll see errors like this and can fix them straight away:
error_reporting(E_ALL);
I suspect the echo early on is causing the problem. Outputting any content renders further header calls redundant.
Try commenting out that line and run it again.
When using header-Location for redirection, be sure you don't let anything echo-ing. the HTML content needs to be empty
URL in header("Location:http://...URL..."); should be full absolute path (best practices)
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).
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().