Christoph M. Becker wrote:
> On 12.01.2017 at 19:18, wart2ww wrote:
>> I did check [the error log] and got the following error message: PHP
>> Warning: Cannot modify header information - headers already sent by
>> (output started at......
>>
>> The sites main menu contains an option to open the same page as the php
>> codes header('location'....
>>
>> Can that be the problem?
>
> If an header is supposed to be sent, but isn't, that certainly could
> cause all kinds of issues. Instead of wondering whether this very issue
> is caused by the failing header call, I suggest you fix it, and then
> check whether the other issue still persists. :-)
Regarding the fix: the problem may not be obvious, neither may be the fix.
As the warning says, the header() call fails because output has already been
sent (and so implicitly at least the default “Content-Type: text/html”
header _field_ – it’s the *P*HP *H*ypertext *P*reprocessor). For example,
<?php
echo 42;
header('Content-Type: text/plain', true);
will echo “42” but not send the specified Content-Type header _field_.
Likewise,
<?php
echo 42;
header('Location: …', false, 302);
will not cause redirection. This has to do with the structure of an
Internet message (see RFC 5322) that an HTTP response is (see RFC 7230);
the one caused by the premature “echo” statement above is at least
-----------8<----------
Content-Type: text/html
42
-----------8<----------
There is no way that more header fields can be sent at this point, because
it is an output *stream*, and we have already output a part of the message
body.
A common reason for this warning is when one does not follow the
recommendation in the PHP-FIG PSR-2 Coding Style Guide to not write the “?>”
at the end of files that contain only PHP code (I would extend that
recommendation to files that *end with* PHP code). [1] Because then any
whitespace that follows *is* output, too. If, for example, you have
configured your editor to add a newline when saving the file (or it does
that without asking), you will cause PHP to generate output; not so if you
omit the “?>”.
Another common reason are error messages or warnings being output before
header(), but we might be able to exclude that here as error messages are
going into the error log.
[1] <
http://www.php-fig.org/psr/psr-2/#files>