What is this PHP warning, "Cannot modify header information"? - php

I'm a newbie for PHP5 and In my php page I'm getting this error when try to redirect to another page
Warning: Cannot modify header information - headers already sent by (output started at <path to my php file>:<line number>) in <path to my php file> on line <line number>
in my php file I have several includes and each one doesnt have any space before of after tag

This is because you must first set your headers and then add any output.
If you did not echo/print anything make sure you did not have warnings or notices (the count as output too if you have error reporting on).
As a good practice, if you can, put the header calls at the top of your script.
You could also take a look into output buffering if you need to generate output before headers.

Maybe your php file has a unicode signature (BOM) which adds a signature at the beginning of your file.
open your php file with a plain text editor like notepad and see if there is something at the beginning of your file. if so, remove them.

The problem could be an opening <? tag with some spaces just before the "<" like shown here "_<". These spaces count as output and can prevent headers from being set.

Related

Is that syntactically correct to omit closing tag in a PHP file? [duplicate]

This question already has answers here:
Why would one omit the close tag?
(14 answers)
Closed 8 years ago.
In some scripts I see that they omit writing a closing tag ?> for the script. Why is it and should I do this as well?
(I'm sure they have not forgotten it.)
Well, omitting the closing tag is just one solution for avoiding blanks and other characters at the end of file. For example any char which is accidentally added behind the closing tag would trigger an error when trying to modify header info later.
Removing the closing tag is kind of "good practice" referring to many coding guidelines.
From PHP: Instruction Separation
The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.
php.net on PHP tags:
If a file is pure PHP code, it is preferable to omit the PHP closing
tag at the end of the file. This prevents accidental whitespace or new
lines being added after the PHP closing tag, which may cause unwanted
effects because PHP will start output buffering when there is no
intention from the programmer to send any output at that point in the
script.
They do it to avoid risking to have whitespaces after the closing tag which may stop headers to work.
This is, of course, true for PHP-only files.
CodeIgniter Framework suggests to omit closing tags for
"... can cause unwanted output, PHP errors or blank pages".
You can read it here.
Modern versions of PHP set the output_buffering flag in php.ini. If output buffering is enabled, you can set HTTP headers and cookies after outputting HTML, because the returned code is not sent to the browser immediately.
Are the examples still valid in this context?
It shows unwanted white space / blank page. HTTP headers do not work for those unwanted whitespace.
Most JavaScript injection is made at the end of the file. It will show an error message and breaks the code, injected JavaScript code does not get executed.

Warning: session_start(): Cannot send session cache limiter - headers already sent

I am getting an error which is very common.
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sabarspinmatic/public_html/testing/index.php:1) in /home/sabarspinmatic/public_html/testing/index.php on line 1
But the strange this is I have only one php file with only 1 line of session_start().
PHP Code is:
<? session_start() ?>
Could anyone know why I am getting this error. I checked the source code and its showing <br />. I don't know why its showing this tag.
URL to check the error is: http://www.sabarspinmatic.com/testing/
//PART 2
I am just using single file index.php without use of .htaccess and only l line of code. I tried by putting php at start of tag but it doesn't work.
First, I would suggest you to use proper starting tag <?php, just in sake of code readability and portability - not all web servers are configured to support the shorter version <?.
Then make sure there is not even a whitespace before the <?php session_start() .... Also some editors (like Windows Notepad) tend to insert some invisible garbage at the very beginning of the file - so I suggest you to open the file in a HEXADECIMAL mode to see if there are really no characters before the <?php .... There could be for example the invisible UTF-8 BOM character.
Last, but not least, use a semicolon ; at the end of the command - no matter it is the only command in the code block, it is simply a best practice.
There me 2 reasons for the ocurrance of the above issue.
1) May in the including files session_start() is already written.
2) May be u are using the session_start() tag in middle of the code.It should be written in the first line of page.
Firstly short tags have been deprecated by php
use
<?php ?>
everywhere instead of
<? ?>
Also is this your entire page? Do you have a space or a line before very first text in the file
From my experience,removing all spaces in your code solves the problem.Especially if you have a white space before
This will cause the error:
..whitespace..<?php
Removing the whitespace solves the problem
I had the same problem... It was because of the Byte Order Mark (BOM) EFBBBF at the start of the file. Use any hex editor to remove these chars from the file and it should work.

PHP end tag "?>" [duplicate]

This question already has answers here:
Why would one omit the close tag?
(14 answers)
Closed 9 years ago.
I've had an interesting phenomenon with a PHP end tag. I had a php file that was executed by an Ajax call. In the php file was included a php library file with assorted functions. When this library was included the php response included a bunch of blank lines. When I removed the end tag from the library this stopped happening.
Can anyone explain to me what going on here ?
This is well documented. From the PHP Manual:
The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.
Omitting the closing tag helps you prevent accidental whitespace or newlines from being added to the end of the file.
That's a core PHP feature: unlike other languages, you need to tag PHP code with a special tag (normally <?php) because everything else is considered literal output:
This is not PHP
<?php
echo 'This is PHP' . PHP_EOL;
?>
This is not PHP either
D:\tmp>php test.php
This is not PHP
This is PHP
This is not PHP either
Although the manual mentions HTML, PHP doesn't really know/care what content-type is outside its tags.
If you forget to close a PHP block when further stuff follows you normally get a syntax error:
This is not PHP
<?php
echo 'This is PHP' . PHP_EOL;
This is not PHP either
D:\tmp>php test.php
PHP Parse error: syntax error, unexpected 'is' (T_STRING) in D:\tmp\borrame.php on line 6
Blank lines are a sort of special case because they are valid and almost invisible in almost all languages (PHP, HTML, CSS, JavaScript...) so they often unnoticed.
Once you've removed the ?> tag, your literal blank lines have disappeared from the script output because they've become part of the PHP code (and, as such, they've started to get ignored).
Of course, blank lines are ignored by PHP but not necessarily by whatever you are generating which, as I said, does not need to be HTML: it can be a picture, a PDF document, an Excel spreadsheet. Bogus white lines can be easily avoided by not closing the last PHP block when it's the last part of the file.

Laravel displays an empty line before the Doctype

<!DOCTYPE html>
This is the code.
How can I fix that?
I tested the HTML/CSS/JavaScript before integrating the code with Laravel.
Make sure your PHP files don't have the closing tags (?>). They might add whitespace to your HTML.
For more info, see the PHP docs:
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.
You might also want to take a look at this post: Why would one omit the close tag?
I know this is few years late but for other people.
change the page encoding to UTF-8 without BOM and it will be solved.

PHP tag closing-- when is needed?

It's recommended that one should not put a PHP closing tag at the end of the file to avoid all sorts of untoward error. But is there any circumstances when PHP tag closing is needed?
A closing tag is needed if you want to switch from the PHP code block to the plain text output.
Here’s an example:
<?php
// PHP code block
?>
<!-- plain text output and not processed by PHP -->
</body>
BTW if you want to know what error you are preventing by skipping the closing tag. Since Zend's explanation doesn't go into detail.
It is not required by PHP, and omitting it prevents the accidental injection of trailing white space into the response.
This means that if you want to use header() to redirect some person to some other location or change the HTTP header in any way... then you can't and will get an error if some file ends like this.
}
?>
//space here
Because then this space will be outputted to the site as content and then you can't modify the headers.
This is my personal "rule":
File with only php code: Never end tag
File with php mixed with something else (I.e. HTML): Always end tag
It's only needed when you want to output non-php code after your php block.
When you are not just using PHP in the script :-)
As a general rule, I always add the closing tag, because it's the only time all day that my question-mark finger gets exercise. That poor question mark gets no love in PHP ;-)
But seriously, adding the closing tag when it's not required can actually lead to really confusing errors. I pulled my hair out all afternoon once because of this. The trouble is usually because there's spaces after the closing tag that you can't easily see, but they get interpreted as part of a response body. This is bad news if you're including this file inside another script that wants to send a custom header later on. You can't send header information after a script has started sending the response body, so these little invisible spaces result in the script failing.

Categories