header() error not shown in php - php

I wrote a PHP program,
I use session_start() and header() functions, I know I should use this functions before I sending anything to client. it's ok, but for test I send a test message to client with echo "test"; before using header(), but I didn't get any error and header function work without any problem !
In previous versions of PHP at this time I will got a message like :
Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:22) in /some/file.php on line 60
I wan't to know why I didn't get any error message ?!
My php version is 5.3.1 and error_reporting is on E_ALL

You don't see the error because
a. output buffering is on
b. the server ignores your error_reporting function because something else instructs it otherwise
run phpinfo(); and see what it says there about output buffering and about error_reporting.

This might be possible because you write into an output buffer (ob_start) which doesn't actually return anything to the client until you explicitly flush the buffer (ob_flush or ob_end_*) or the script ends.
The fact that you can't send headers after you begun writing the body part of your HTTP response is not a php but a HTTP protocol limitation, so this cannot be version specific either.
Many frameworks like the Zend Framework use output buffers so a developer doesn't need to care about the order.

I found the problem,
I don't use any FrameWork.
the problem was error_reporting('E_ALL');
I wrote this code on first of my program, and so PHP didn't return any errors !
I deleted this line and I get the errors !
This is kidding because this line said to PHP to show ALL errors !
I don't know why PHP don't show any errors.

Related

headers already sent, file is cached, modifications don't apply, how to refresh it?

I am trying to install Chamilo on my server but I get a warning and the install is cancelled... I thought warnings were not like errors and wouldn't make it fail, but either this warning is important or something else is wrong that I don't know of.
The warning is a "headers already sent".
I have followed the steps to solve it to no avail (I commented the lines with echo, used ob functions and even php.ini buffer setting but it keeps displaying this exact same warning).
It comes from script that otherwise work fine on my WAMP test server and I found no complaints about it on the chamilo threads... I'm not even sure this could make the whole install crash...
Warning: Cannot modify header information - headers already sent by (output started at /var/www/new_chamilo/main/install/index.php:391) in /var/www/new_chamilo/main/inc/lib/template.lib.php on line 1038, referer: XXX/new_chamilo/main/install/index.php?running=1&installType=new&updateFromConfigFile=
[...]
header() /var/www/new_chamilo/main/inc/lib/template.lib.php:1038
So the two lines mentioned here are :
header() /var/www/new_chamilo/main/inc/lib/template.lib.php:1038 (the closing })
if ($sendHeaders) {
header('Content-Type: text/html;
charset='.api_get_system_encoding());
header(
'X-Powered-By: '.$_configuration['software_name'].'
'.substr($_configuration['system_version'], 0, 1)
);
}
and
index.php line 390
success: function(datos) {
if (datos == 'required_field_error') {
message = "<?php echo get_lang('FormHasErrorsPleaseComplete') ?>";
} else if (datos == '1') {
message = "<?php echo get_lang('ContactInformationHasBeenSent') ?>";
} else {
message = "<?php echo get_lang('Error').': '.get_lang('ContactInformationHasNotBeenSent') ?>";
}
alert(message);
$('#license-next').trigger('click');
$('#loader-button').html('');
}
I have tried commenting these error echoes, buffering with the ob functions or with the php.ini, nothing works, I still get the warnings and the install fails (I am not sure the install fails because of it though, but I have no other message whatsoever).
On a side note, the php.ini says display_errors = Off but I still get that
I have followed the headers sent tutorial even before posting here actualy, but it changed nothing.
As you can see in the code, there is "echo" and I suspect it is the problem so I commented the whole function (it is only about sending contact information to chamilo so it's quite useless) but the exact same error, pointing to the commented lines, still occurs.
I am wondering if my file hasn't been cached. Is there a way to force refresh of this file only ? I see that htcacheclean can clear a specific URL from the cache, but I don't find examples of how to do so. Anyone knows how to provide a URL as parameter to this command ?
Actualy I know I had to install some extensions that caches scripts for more efficiency, because it's absence triggered many warnings. Is there more than one cache server side ?
Any insight about this would be very welcome...
Thank you in advance for your time !
First of all, the Headers already sent message is usually symptomatic of something else that goes wrong. Indeed, Headers already sent only appears when something (in PHP) tries to send HTTP headers but... they were already sent. The issue is usually that the headers were sent when they shouldn't have, so looking at the place where the error triggers is usually incorrect.
Second, the display_errors = Off might actually be what's preventing you to see the real problem, although sending the headers is not always the result of an error.
If you have access to your server's error_log, it is probably much clearer about what the error is (usually, for Chamilo's installation process, a matter of the PHP version which should be 5.4 or higher, or of the date.timezone setting that would not have been set, or any dependency that would not be matched). It would also be interesting to know on which page of the installation process this appears. It might make it easier to debug.
Given you mention the block with the AJAX call, it is probably linked to the contact form, which is not mandatory to fill and send (so you could skip it altogether).
The Headers already sent message is a warning, so it might not block the installation completely. Maybe you can just ignore it.
Finally, if you are upgrading from a previous version of Chamilo instead of installing a new one, you might want to clear the template cache (which you can do in version 1.10 and above with rm -rf app/cache/twig/*.

'php headers already sent' no longer an issue?

In the php manual (http://php.net/manual/en/function.header.php) I find the following about http headers:
QUOTE
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.
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
ENDQUOTE
I tried this now (making a study of the HTTP protocol) and find that this doesn't seem to be an issue anymore; I wonder what has changed: has the way php deals with parsing webpages changed? I tried several browsers (Chrome, IE10, IE7) and OS (Windows 8.1, XP) and none of them seem to have an issue with this anymore (see informaticaschool.net76.net/httpdemo3.php for a page that includes exactly the above code example, not giving an error.
For the details of the php version running on that server (I also tried a different apache server) see informaticaschool.net76.net/phpinfo.php.
I admit its a bit of a strange thing to ask why an error does NOT occur but as pointed out i'm studying the workings of http. Looking into a Wireshark trace now to find out what happens exactly in this case, that I knew to be an issue before, I am now surprised to find that NO error appears (and not finding an explanation for it in the php manual).
That server has output_buffering turned on, so all output is buffered and held, which allows you to send headers to the web server even after you have already produced output. See the canonical answer about this issue, section "But it worked on the other server!?".
You must send headers before send output buffer.
Or you can use functions ob_start and ob_end_flush to save output buffer.

Joomla Plugin Warning on the production server - "Cannot modify header information"

I'm creating a plugin for joomla and it is working as expected in my local Windows 7 machine with WAMP. But when I load the plugin into the production server(Debian), I get this warning (not always):
Warning: Cannot modify header information - headers already sent by (output started at /httpdocs/plugins/system/fiuser.php:1) in /httpdocs/plugins/system/jat3/core/parameter.php on line 73
I tried googling, but it didn't helped me solve the problem actually.
<?php
defined('_JEXEC') or die('Restricted Access');
class plgSystemFiUser extends JPlugin {
// Some functions
}
I get this warning whenever I delete the browsing data from the browser and then the problem persists until I close the browser or go to an another site.
Content of line 73, parameter.php:
setcookie ($this->template.'_tpl', $this->template, $exp, '/');
I'm finding it difficult to debug this problem, as I'm not too much experienced with Joomla and PHP, so any help is appreciated a lot.
Remove the closing ?> tag at the end of your PHP files. It actually serves no useful purpose as the PHP interpreter knows that end-of-file means end-of-PHP too. Removing it means that any extra blank characters added by your editor will have no effect on the output generated and so will not prevent additional HTTP headers from being sent.
Turn output_buffering setting on php.ini to on to permanently remove this error
There is a conflict of the header() method, take a look at flushing the output buffer
PHP.net Output Buffer methods

PHP Header("Location:...") works on one computer and not another

So I just got a nasty surprise when I deployed some code I thought I'd tested. It would seem there must be some difference between my test machine and my server. The exact same code, featuring a header redirect, worked perfectly on my test machine and not at all on the server. The redirect on the server simply didn't happen, leaving a blank page as a result.
The header is called somewhere in the middle of the script - but nothing will have been output yet. It doesn't output anything until the very end of the script. Long after everything else is run. It buffers everything.
Both server and test machine are running the same PhP version, the same Apache version. Is there something in the configuration files that would allow the header to happen for one and not in the other? Is there something else going on here that would cause it to fail?
EDIT:
Here's the line that sets the header:
public function setRedirect($url) {
header('Location: '.$url);
}
And here's the code that calls that:
$url = new URL('index');
$this->layout->setRedirect($url->toString());
Where URL::toString() always generates a fully qualified domain name, in this case: http://domain/index.php?action=index
I checked both Php and Apache error logs. Nada.
Probably there was some whitespace or other form of output before the header call.
This is only work if you the ini setting output-buffering is on (or if you explicitly start output buffering, but in that case, the redirect should work in both computers).
You can confirm this by turning on error reporting.
Use Fiddler or some other client-side tool to check your headers. Determine that the Location: header is actually being sent. Also, some browsers are picky in the order that headers need to be sent.
I think the most likely explanation is that an error is causing the script to exit on your server, and you have display errors turned off (hence the blank screen). I would suggest checking the Apache error long on your server to see if PHP is putting something in there.
Otherwise you could use a browser extension like LiveHTTPHeaders (for Firefox) to see if the location header is being sent at all, or try debugging the script to see if it's even getting as far as that header call.
I think your server puts some script in your pages to track visitors and give you traffic stats or for a similar purpose. Ideally, you should get an error for this but may be your server has error reporting disabled which gives you a blank page.
I suggest you to run a script with a syntax error and check weather your server has error reporting disabled.

Cannot modify header information, headers already sent [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Warning: Headers already sent” in PHP
I tried using xampp lite portable in my flash drive so that I could bring the server anywhere.
But I always get an error when using xampp. This error in particular:
Warning: Cannot modify header information - headers already sent by (output started at /path/to/geeklog/public_html/blabla.php:581) in /path/to/geeklog/public_html/system/lib-blablab.php on line 180
I don't know why I'm getting this error. But when I use wampserver. There's really no problem. And I don't get any errors. What do I need to do to solve this when I'm using xampp.
Is there a portable version of wamp
To ensure your error reporting level is the same across environments, you can set it in your application using error_reporting() and ini_set('display_errors', 1)
Also check your .php files for any whitespace before the opening tag and after the closing tag.
In addition to the points mentioned above, ensure you are not outputting anything before the headers are set, for example the following code would produce an error similar to the one you are receiving:
echo 'Hello, World';
header('Location: http://www.somesite.com');
Is it possible there's a little bitty bit of whitespace outside of the PHP tags in blabla.php line 581? I bet there is.
See, if you have anything outside of PHP tags, that is sent to the browser. And once something has been sent to the browser, you can't send headers (like the sessionID cookies!) anymore.
The problem is likely not XAMPP, but your PHP code as you put it on the flash drive.
The difference you're seeing between environments is most almost certainly a difference in the configuration.
One of two things is happening on the server that isn't emiting Warnings:
1) Output buffering is on by default
2) error_reporting and/or display_errors is set so you're just not seeing the warnings. But if this were the case, your headers still wouldn't get set, so it's probably #1
You can check these settings by looking at the output from phpinfo()
Output buffering, when enabled, buffers any output (regular content not inside tags, anything you echo or print(), etc) on the server and then sends it to the client in one shot.
I'd poke around in your portable version, find php.ini, and try turning output buffering on. Alternatively, you can turn output buffering on at runtime by sticking ob_start() near the top of your script.
The error states: output started at /path/to/geeklog/public_html/blabla.php:581, so I would start there. You need to send all headers before outputting anything, this includes whitespace as mentioned by others here.

Categories