Where is the data coming from? - php

It seems I met this problem before, in a different disguise. When a file was being "included" in my main php file before starting a session in that same main php file, I'd get the "headers already sent" error because my included php had a blank line at the start, which was being somehow sent before anything should be sent. Delete blank line, "problem solved"...
Now I have this ajax thingy where I scan a certain string returned by a php. The string I hardcoded in this php to be returned to the ajax callback was the string "alert", which was never seen. I scan what is being received by the callback, and guess what? It has a character 10 starting my 5 character "alert" string.
Sounds familiar? yes, I had an include before the "echo("alert");", and that include'd file DID start with a blank line (too! why I keep doing this???). Delete line, and now I don't get the character 10 (ascii "new line" eh?) starting my "alert" string anymore.
The question: why is the php echo'ing a "new line" character that was never formally "echoed" along with my carefully crafted string? Is this a bug of mine or php? Thanks in advance.

Everything not inside your <?php ... ?> tags is treated as text and sent to the client. So make sure not to have empty lines in your source files...
Edit: The purpose of this is to have a simple way to have a HTML file mixed with portions of PHP code without the need to echo all the HTML stuff explicitly...

Related

Cannot modify header information (connot detect header)

I checked all answers in different pages and use it, but the error still appears. Please help me.
The error is:
Warning: Cannot modify header information - headers already sent by (output started at /home/iraustor/public_html/copytest/post.php:1) in /home/iraustor/public_html/copytest/post.php on line 57
The URL of form is here: http://iraust.org/copytest/contact.html
And the page that after complete the form is: http://www.iraust.org/copytest/thanks.html (or any other method to shod this message)
It has taken 2 days but answer. Please help me.
"header("Location:$Redirect_Page");"
If you issue headers, like you do for a redirect (setting Location) you MUST be sure that there's no other output before that statement, as PHP will already build the headers (however maybe not yet flushing them to the client) on the first output.
This might be the case for several reasons (unexpected error in some require, a whitespace at the beginning or end of some file, etc, but the error message you have is clear in mentioning where this output started: /home/iraustor/public_html/copytest/post.php:1.
You should double check that there's nothing before the opening and after the closing <?php ... ?> block. This applies to all included or required files in called script.
As pointed out by h7r, if you use the header function you cannot print anything before its call.
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.
From Header documentation on PHP.net
So, the first thing you should check is the line 57 in your post.php file: there starts the output that causes the error.
Be sure that no output is sent: also a white space or a blank lines is an output and this cause the error.
If you like, you can use the output control functions to buffer the output: in this way you can print what you want but all your outputs aren't sent immediately to browser, so you can use the header function without causing errors.
Put your code somewhere for us to look at...
Possibly PHP could be outputting an error, or a warning, etc... It might not be you for example.
no space before
I completely confused!
The form is working and the information will be send to e-mail
but the error makes feeling nervous for users
The problem solved by simple code editor (notpad++)
problem: hidden white space and non Unicode characters

PHP Scripts Encapsulating Returned JSON Array with Number Above and Number Below

I'm having a problem where my PHP scripts are returning my json encoded array with a number above and a number below it. Like follows:
26
[0,"edited_token_string"]
0
I have not changed any of the scripts that I'm encountering this on, but it is happening with all of them. I don't have any other echos other than the one used to echo the array. Our server was returning "null" from all of the scripts all morning and now is returning the correct array, with these numbers surrounding it. Is it possible something was updated on the server that accidentally turned on some type of debugging? I've called our hosting service, but they are incredibly unhelpful.
Thanks in advance,
Max
Try adding header('Content-type: application/json'); directly above the line that outputs your JSON. If something else has already outputted something, you will get an error telling you where in your code that happened.
For the undesired output after the JSON, could it be that there is also an extra space after the closing ?> tag? A quick and dirty fix would be to just add die; after the last intentional echo;

session_regenerate_id and setcookie on same page in PHP

Ok, so I am working on this login system and of course when the user logs in i regenerate the session id.
But after I regenerate my session id i also want to set a token to be stored in a cookie. However I seem to not be able to do that on the same page. I get an error that says:
Warning: session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent in /htdocs/somesite/test.php on line 44
Warning: Cannot modify header information - headers already sent by (output started at //htdocs/somesite/test.php:76) in /htdocs/somesite/test.php on line 54
This what I am doing right now:
session_regenerate_id();
setcookie("Foo","Bar", time()+$CookieExpireTime,"/");
I am wondering if there's a work around this. It's working when I'm testing it on localhost, but not on the server...
Common Cause:
This warning is often caused by a blank space or extra line at the beginning or end of a .php file.
Check the error for the filename that generated the error (ie: the "output started at...." filename), open that file in your text editor and remove the extra spaces or lines immediately before the first
Other Causes: Syntax errors
In the example above, you'll see output started at /....includes/something/something/something.php:12. The includes/something/something/something.php is the filename you need to be concerned about. The :12 means that the problem you need to fix is on line 12.
In that same example, the "in /includes/something_else.php on line 67" message can be completely ignored. It is not the problem. It is the one that discovered that the problem had already occurred.
If the "headers already sent" error appears AFTER any other error, then you need to fix that other error FIRST. (The error message itself is what caused headers to be sent, so fixing that error will cause the second error to go away too.)
If the "started at" refers to line 1, then it's either a space before the opening <?php tag or it's incorrect encoding on the file.
Remember, if your language is in UTF8, then you MUST encode the file as "UTF8-without-BOM", else the BOM (byte-order-mark, an invisible character at the beginning of the file) will cause this same headers-already-sent error.
Summary:
a) look for where it 'started at'
b) track the line number
c) check what's normally happening on that line.
If it's the end of the file, then it's blank spaces.
If it's the start of the file, it's likely spaces or incorrect encoding.
Elsewhere it could be a syntax error or the result of an "echo()" statement which is displaying info or perhaps debug code.
Common syntax errors include the use of single-quotes inside statements that already have single-quotes. Check to be sure your quotes aren't mismatched. If you need to use single-quotes while inside other single-quotes, change yours to \' instead of just '.
d) the rest of the info simply shows other execution information, mainly the part of the code that discovered that it cannot proceed as expected due to the problem that happened in the 'started at' location.
Source: zen-cart.com
Another similar thread on SO: How to fix "Headers already sent" error in PHP
I hope this helps.
The problem isn't with the two functions, the problem is that you cannot call session_regenerate_id() once any output has been sent to the page. That function, like header() has to be called before any sort of output is sent to the page.
Either re-structure your page so that this function is called before any output is sent, or use output buffering so that this function is called before any page content is sent.

Line feed is showing up unexpectedly in PHP

I have spent the last several hours pulling my hair out trying to figure out the solution to this problem. I am sending an AJAX request which, up until some minor changes, worked perfectly, returning a lovely usable character to the Javascript. Now, however, a \r\n is being returned, and I have spent far too long tracking it down. My final method for finding where it was being included was literally echo-ing "OMG" in various places around my scripts until it showed up on Line 2 of the HTML instead of Line 1. Here is the offending script:
// Import Global Game Variables
include('../engine/engine_core_functions.php');
// Convert our gamestate(gameID)
//$curGamestate = getCurrentGamestate($gameID);
// Make sure it's a valid turn
if(isMyTurn()) {
// Draw a card from the card drawing mechanism
$cardValue = drawCard();
$cardValue = str_replace("\r", 'R', $cardValue);
echo $cardValue;
}
else echo 'Error 3';
The line skip occurs immediately after the include file at the top. Before the include, no line break, after the include, line break. So I go to the include file. Placing my
echo 'OMG!';
at the VERY END of the included file does NOT produce a line break. Which led me to believe that including a file may (why!?) generate a line break (it's 5 AM...). However, there are multiple included files at the top of the offending included file. None of them generate breaks. The entire "engine_core_functions.php" generates no line breaks at all.
However, a break shows up when it is included in the above-shown script. Needless to say, I'm baffled and extremely annoyed. I could simply remove the offending characters (via PHP or Javascript) but it annoys me I can't seem to fix the root of the problem. Please help, thank you.
You could have some kind of invisible BOM mark at the beginning of your file or something else.
Always let <? or <?php be the first string of your PHP files and make it a practice NOT to end the entire PHP file with ?> if it's going to be included by another file.

PHP warning: headers already sent in Unknown [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I'm looking for things that might trigger the following PHP warning:
PHP Warning: Cannot modify header
information - headers already sent in
Unknown on line 0
Turned out that it was the line
ob_start("ob_gzhandler");
that caused the warning. This has been reported and fixed in 2001, it seems, but for some reason it keeps coming back.
It might be a lot of things, but as the others said, it's often just a space lying around somewhere that gets outputted and then a header() command is sent which normally is fine, but not after starting to send content back (potentially just a space in this case).
Using ob_start() stops the output from going out right away by buffering it. So it's a potential solution, or at least way to diagnose where it's coming from as zodeus said.
One common thing that causes those lose spaces are in this scenario.
global.php
<?php
$variable = 1;
$database = 'something else';
?> <-- A space here
<-- Or here
index.php
<?php
require('global.php');
$var = dosomething();
header('Location: http://www.example.com/');
?>
One way to fix that is to remove the ?> at the end of the global.php file. You don't need those, they are only useful if you start putting HTML for example after your PHP code. So you'd have:
<?php
$variable = 1;
$database = 'something else';
And when you do the require(), the space is not outputted before the header().
Just to illustrate the problems with content outputted and headers is that other common case that gives a similar error. It happens when you forget to stop the processing after a redirection using header().
if ($notLoggedIn) {
header('Location: http://www.example.com/login.php');
}
echo 'Welcome to my website'; // this will be outputted,
// you should have an exit()
// right after the header()
I think whats happening is one of the built in php functions is outputting something. I've seen this with a couple of the IMAP functions where they out put just a " " (space character) and it screws things up.
You can thry tracking it down using Xdebug or the Zend debugger, but i f you have neither
try wrapping output buffering around some of the functions you think may be cause it.
ob_start();
callYourFunction();
ob_end_clean();
Do this one function at a time and when the error goes away you'll know which function is cause you the problem, then you can either file a bug report or just leave it in as a hack. But at least then you know what function is cause the issue.
Edit: The fact that is says your output is occurring on line 0 means that it's a C level function doing the output, not any code that's been written using PHP.
Have you checked your files for unintended UTF-8 BOMs?
The error tells you that something has sent output, which would force headers to be sent, because the headers must be written before the body of the http message.
The most common problem I have found is text in headers. vis:
<?php // myfile.php
include 'header.php';
?>
and in header.php:
<?php // header.php
....
?>
What you can't see here is that there is a blank - either a space or CR/LF after the closing '?>'. This is output because the php standard says that anything outside the php tags is output as html.
The solution is to make sure that you make sure to erase everything after the closing '?>'

Categories