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.
Related
I have a Web application in PHP which when / (or something else) of it is opened in a browser displays the following error in the browser: Parse error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE) in /var/www/n_environment.php on line 3. The file n_environment.php is loaded with require_once 'n_environment.php'; in index.php. And also from different places. It contains just comments, calls to define (unconditional or guarded by if (!defined) and two assignments of arrays of strings to variables.
I thought maybe it's (indirectly) in a class but not in a method, which indeed PHP prohibits for require_once, but apparently that's not the problem here. If I put anything else as the first instruction, e.g. echo or an assignment, a similar error (with a different offending token) results.
If I comment out everything in the file except the initial <?php, the error changes to unexpected end of file.
And if I also remove the <?php, what I get in the browser is the file content (block comment with code inside) with รข instead of new lines.
What are the possible, most likely reasons for such behaviour? And how to fix it?
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...
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
The relevant peice of code is below. According to php.net I have to make sure there is no output, not even any whitespace. There isn't any. the php tag is the very first tag in the document no whitespace preceding it. What am I doing wrong?
<?php
// main.php
// 6:48 PM 8/6/2010
include('config.php');
// Does myid cookie exist?
if ( !isset( $_COOKIE['myid'] ) )
{
// Generate myid
$myid = substr(md5(date( 'Ymdhis' ) . str_replace( '.', '', $_SERVER['REMOTE_ADDR'] ) ), 0, 10);
// set the cookie
setcookie( 'myid', $myid, time() + 31536000 );
There needs to be no output at all before calling setcookie, even from other scripts.
If config.php had whitespace before the opening <?php tag (or after the closing ?>), for example, that would cause the problem.
Hunt down where header is called or some text outside of PHP tags (even whitespace) exists. That's what's happening, no doubt.
Some text editors don't show all symbols. Please try to open your main.php and config.php un different editors and check for something before <?php in both files. I can't tell about Windows, but on Linux you can use vim or vi. Also, full error message is telling you where exactly error started.
You fail to read or at least to paste this error message here. It has DETAILED explanation, what are you doing wrong.
most likely it would say output started at config.php:XXX. Pretty clear.
if it says output started at main.php:0 - this is most likely a BOM character, and you have to re-save your file without it, using your editor Save dialog or another editor.
Always read entire error message. It is not only says what something went wrong, but often explains, what certainly happened.
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 '?>'