Is there any alternative available to
<?php ob_start(); ?>
Codes go here..........
<?php ob_flush(); ?>
for eliminating Warning: Cannot modify header information - headers already sent by (output started at............
I'm looking for an alternative .htaccess rewrite rule to the above PHP functions.
Thanks,
You will be unable to fix that error by editing your htaccess file unless you hide the error which won't fix your actual issue because the header change that you attempted will fail.
Instead, you need to ensure that you modify the headers before you output any html, text or white space. This error can often be caused by:
white space before your opening php tag
outputting some html (such as the docytpe and head) then attempting to change the location header to redirect to another page
The true "alternative" (which really isn't an alternative, but is instead the right way) is structuring your code so that no header is set after the output has started.
There is really no case when this is not possible, and output buffering is not meant to be used as a workaround for this.
An alternative and simple resolution to this problem is to enable output_buffering variable on the PHP server which can be easily done by saving the following code in php.ini file.
output_buffering = On
Related
Does somebody know why my header() does not redirect?
The last part of my script is:
header("location: test.php");
die('died');
It writes:
died.
:(((
It should has to redirect before it dies, but it does not.
Do you have you any idea?
It's probably that you're calling header() after you are outputting some text/HTML to the browser, which is a no-no. Your header() call will be ignored if even so much as a single space of output has been sent before the call.
In other words, your header() code needs to be at the start of your script, before you display anything to the user. If that still isn't working, make sure you don't have any spaces or other whitespace by mistake outside of your php tags.
Maybe there is some invisible output before header, which is preventing setting header, and informative warnings are suppressed.
Additionally Location-Headers should contain an absolute URL:
// Show Errors
error_reporting(E_ALL);
ini_set('display_errors','On');
// Redirect
header('Location: http://example.com/path/test.php');
die('redirect');
You should use a fully-qualified address for your location header, and not output any content:
header('Location: http://example.com/test.php');
die();
Additionally make sure that no other content was sent before setting the header, otherwise it wont be sent as the headers will have already been sent to the browser.
location: should be Location:.
Moreover...
Scroll to the top of this page.
Click on your account name.
Click on a random question
Check mark a random answer
Come back to find more serieus answers
Good luck.
Just resolve it by removing ALL things before the php tag.
Remove the SPACE before the php tag
Select all before the tag and then push the erase button.
I was stuck on this error, too, and tried to show errors with error_reporting(E_ALL),
but this didn't worked for me. Info: I was using a hosting service on Strato.
The solution to all this was answered by Floem, thanks for this great setup.
If you ever don't get errors in case you put error_reporting(E_ALL) once in,
-> You must additionally add ini_set('display_errors', 'On'), to force PHP to show all your Errors. This solved my Error issue on
Then it's possible to read out, why PHP wouldn't make your redirect.
One tip on my side: I would recommend you to build a little redirect Class, as shown in this Video: Redirect Class for easy use, if you're going to use this redirect more than usual.
If the file containing the 'header()' instruction is required directly/indirectly in a HTML file, then the instruction {include 'someFile'} should be the first line of that HTML file. Place this instruction on the top of your HTML file, as shown in this image.
Add
ob_start();
before
header("location: test.php");
I have language related Constants defined in a php file, that can be edited from admin side, After editing the contents, any page that include it, failed to successfully redirect using php header function.
As error reporting is off, it don't generate warning message and same page refreshed after form submission instead of redirecting to another page.
Any help.
There is probably some output already being sent to the users browser when you're setting the redirect header.
Setting HTTP headers after sending content won't work. You'll have to suppress any output until you sent the header.
Above answer is correct. check whether headers are already flushed with header_sent
For debugging you can see where the mistake is:
<?php
if( headers_sent( $in_file, $at_line ) ){
trigger_error('Cannot write redirect header, output started at '.$at_line.' in '.$in_file, E_USER_ERROR );
}
Likely mistakes causing early output are:
Accidental text entered before <?php at beginning of files.
Accidental text after ?> at end of files. (not needed and should be omitted anyway)
Error output. (especially notices raised - likely if using Wordpress)
use following function to resolve this problem:
ob_start();
replace the header with javascript redirection
echo '<script type="text/javascript">window.location="membre.php";</script>';
Are your files' encoding "UTF-8"? May be, your text editor added BOM-record in UTF-8 file. And it crashed PHP header function. If you use UTF-8 encoding, you should save file as "UTF-8 without BOM"
I can't handle this error, please help me. It worked on my laptop but did not work on my desktop.
Why?
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at F:\xampp\htdocs\etest\index.php:1) in F:\xampp\htdocs\etest\common\header.php on line 3
The code:
<?php
ob_start();
session_start();
include("constants.php");
include("includes.php");
?>
Thanks for your kindness!
the error was at F:\xampp\htdocs\etest\common\header.php on line 3 but output was already started at F:\xampp\htdocs\etest\index.php:1
I assume you posted the header.php, but your index.php either has whitespace before the
<?php include() or outputs something other than a header on the first line.
the error is caused by line one of index.php, whatever is there. I'd guess whitespace.
In general, when you see "headers already sent", it means that you are trying to do something that requires an HTTP header to be set, but you have already sent content to the client. When using HTTP, you have to send the headers, then the content comes after. If you've sent content, you've missed your opportunity to send headers.
Start the session on top of the file.
direct afer the php tag when you make an echo or send some header before you get this errormessage.
<?php
session_start();
ob_start();
include("constants.php");
include("includes.php");
?>
Enable output buffering by configuration. This way it will start before anything else, so it certainly solves your problem. Although you should look at your code and find the cause of the present error, because it seems you do not fully understand your site yet.
After enabling output buffering by configuration, you no longer need to call ob_start() manually.
Create a file called .htaccess with this content:
php_flag output_buffering on
... or add this line to your php.ini:
output_buffering = On
use_only_cookies=0 to use_only_cookies=1 in php.ini file
I encountered the same problem in xampp 7.0.1 and this fixed it. Read it here.
Yes start the session on top of the file amd make sure you do not have a space before
<?php
Plus you can call session_start(); only one time
session_start() must be called before there is ANY other output. The error is telling you that output started on index.php line 1. The call to session_start() must be placed before that.
Put this code inside includes.php
ob_start();
session_start();
And remove the same code from every other page.
Also make sure that in your files there is no whitespace before the initial
(specifically you are getting the error because something is wrong on the first line of index.php not of header.php)
I was getting this even when I had it at the top of the file.
Problem INVISIBLE UTF8 CHARACTER
Solution
Was to delete the file and make a new one starting with <?php and then copy everything after the php into the new one. (Don't copy the beginning of the file).
I have created a PHP application with output_buffering = On.
Now, if i do output_buffering = Off then i have get following error
"Cannot modify header information - headers already sent by ".
How can i remove this error?
What can be the drawback if i upload application on live site with output_buffering = Off?
From PHP Tag Wiki:
Q. "Headers have already been sent..."
A. You're outputting content and triggering PHP's default content-type:text/html header before making your own call to header(). There are a few ways this can happen, check for a stray echo or print, whitespace before and after the tags or maybe a Unicode BOM. Consider using output buffering. With many scripts, there is no need to include the ending ?> and the problem is easily fixed by removing the ending ?> tag from your files.
There is also a myriad of duplicates for this.
Send out your headers before any output goes to the browser.
header('Foo: bar');
echo "foo";
If you turn output buffering off, you are not allowed to send any additional headers (this includes cookies e.g.) after you've send any piece of data to the client (namely using echo or some other output function).
You can use headers_sent() to check where you started to output data to the client.
Ensure all header('') tags go right at the top of the script with no white space!
EG:
<?php
header('type of header');
?>
If you really have to do it further down the page you can call ob_start() at the top of the script. Call ob_end_flush() at the end of the script too.
Going through the php.net site, it had an example for header, which says would give me error. I copied it, and executed on on WAMP, but it didn't showed me any error, but did redirect to the site.
<html>
<?php
/* This should give an error (but it doesn't!). Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
?>
Just wanted to know, if its a right behavior on my WAMP, or its an error, or if I have any particular settings active in php.ini file which is making this work!!!. Let me know if anyone needs my php.ini to be copied here!!
Thanks,
Tanmay
It sounds like you have output_buffering enabled.
http://php.net/manual/en/outcontrol.configuration.php
Standard configuration would be to error because data has already been output, and headers need to come first. Output buffering would allow headers to appear in code after other output, but it would still output the headers first due to the buffer.
Headers are sent as soon as any text is sent to the browser, and can only be sent once. once the is sent, headers are sent along with it, so trying the header function after that wll throw a headers already sent error.