The php documentation suggests that I should end each ob_start() with an ob_end_flush(). I am using one on each page of a site, just to allow me to use firephp log methods anywhere in the app.
the app works fine, but I wonder if there is anything I don't know that might be detrimental.
I think the reason for this suggestion is, that PHP flushes your output buffer implicitly when not using one of the ob_end_* functions. While not an error, this can cause problems when not expecting it. The PHP-docs try to protect you from these kind of problems. If you are aware of the implicit flush, it is probably not an issue.
You can use Output Buffering as
<?php
ob_start();
echo "Some text you want to echo on page!!";
header("Location:somepage.php");
ob_end_flush();
?>
The problem is that we cannot send the header after we start sending the output. To solve this we buffer the output. The function ob_start turns output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. So the echo output will be buffered. Next we send the header without any problem as we've not yet spit out any output. Finally we call ob_end_flush to flush the internal buffer contents and to stop output buffering.
if there is loops, for example, some one can start buffering. And calls your function. When he tries to ob_end_flush code gathers your contents.
Related
I have a script that has a call to header(); and its working fine for a couple of days since I first started running the script.
then after a couple of days, it started to have an error saying it cannot modify header information.
Then I put ob_start(); and ob_end_flush(); upon googling the error and it works!
The error has gone but my question is that why it works without ob_start(); and ob_end_flush(); for a couple of days before?
I want to know the explanation behind this behavior.
btw, I call header() this way:
if(condition is true){
header('Location: anotherpage.php');
}
and I have a session_start(); at the beginning.
Previously, you had no non-header output before the header line. Now, you have non-header output before the header line. That will only work if the output is buffered so the header can be actually output ahead of it.
You can not output any data before a header!
ob_start() is a output buffer and will buffer all echoed data and print it after all headers etc.
PHP flushes the data when the script is finished automatically so there's no need of the "ob_end_flush()"
You have some code outputting something before header() is called. It can be a print, echo or similiar statement, or even a white space before <?php.
The reason why ob_start causes the error to go away is because it causes any output to be buffered, therefore defered until the moment you call ob_flush.
This way it's guaranteed that the headers will come before content, even if you mess with the order of the commands in the code.
I have written a class which outputs content type information as a header, however this does not work. After reading PHP.net it states
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.
It is a very common error to read code with include(), or require(),
functions, or another file access function, and have spaces or empty
lines that are output before header() is called.
I am unsure what to do as I cannot hard code the content type everywhere needed. Does anyone know if it is possible to set headers in functions / classes at all?
The restriction is that the headers must be sent before any other output is sent.
The easiest solution would be to enable output buffering. So you can set headers anywhere till the buffer is flushed.
You can do this ether by using the function ob_start
Or using the output_buffering ini directive in php.ini file.
As pointed out by Brad, it is not the most efficient solution as it takes up server memory to buffer the output and delays the sending of first byte.
The best solution would be to find what is sending the output before the header call and delay the output if possible.
A good trade-off I found was to enable output buffering using ob_start at beginning and flushing the buffer just after the page <head> element is generated and all the needed headers are set. This way any further output is not buffered. The only thing to take care of is that all header calls are made before the ob_end_flush call.
The headers were sending correctly all along but I was checking the response of the file I was using and not properly getting the response from the CURL, if that makes sense! I just needed to use:
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
so apparently if you do this:
<?php
echo 'something';
header("Location: http://something/");
?>
it will not work because there is an output preceding the header...
is there any other alternative php redirection method that works straight from php without installing anything and in which it will still work even if there's an output preceding it so that I don't have to worry about making sure that there is no output before, etc...
not, unless you do something in javascript or html tags in the page that you output itself
if preceding output is a problem
you can also use output buffering, see ob_start, ob_get
to get around that
There is no other way to do a php redirect, but you can fool it to still work even with code prior. You would buffer the content and only output it if there is no redirect or reaches the end of the script. Note: this may be resource heavy in some cases.
ob_start()
....CONTENT...
ob_end_flush();
There are no ways in PHP except using header()... before output is sent (headers be already sent)...
You can either use meta refresh in HTML that is set at zero seconds, or javascript.
But I wouldn't recommend javascript as some will have it disabled.
You could use a meta refresh tag.
You understand why this is impossible, right?
As soon as you echo "something" you have sent content to the client, and as part of that client headers were already sent. You can't retroactively modify headers you already sent, and you can't make two responses to one HTTP request.
ob_start() and ob_end_flush() will buffer the output instead of sending it to the client, which will allow you to get around this problem, BUT
a better solution would be to:
separate your logic code from your template so that you don't write anything to the screen until you already know you aren't going to redirect.
I'm trying to design a page which does some database actions, then redirects to user back to the page they came from. The problem is that I use a require() function to get the connection to the database, so the headers are already sent. A meta tag is out of the question since I want it to look like all the processes are done from the page they came from. Any tips? Is there a way I can use the require() and the header() or do I have to drop one? Is there an alternative to header()?
If you can't send the header() before some content gets sent, use output buffering by placing an ob_start(); at the beginning of your script before anything is sent. That way, any content will be stored in a buffer and won't be sent until the end of the script or when you manually send the buffer's contents.
On another note, simply requireing another file would not generate any headers/content unless that included script sends them. The most common "hidden" cause of this is unnoticed whitespace before or after the <?php ?> tags.
As Artefacto noted, connecting to the database should not require any output. Fix whatever you're including (e.g. database_connect.php) not to output. See this search on the "headers already sent" issue, which may help you find "hidden" output.
ob_start(); // start output buffering
echo "<html......"; // You can even output some content, it will still work.
.
.
.
.
.
header("Location: mypage.php");
ob_flush(); //flush the buffer
In this case, all output is buffered. This means, the headers are processed first, then the output comes to play...
You cannot send any headers after some content has already been sent. Move the header() call to be before the require() call.
You cannot send headers after any data has been sent to the client.
However, using require does not meen that you output something. If i understand your right, you can include your database files, run your queries and then redirect the user. This is perfectly valid.
If you need to send some output (why if you need to do a redirect?) another option is to use output buffering. By using output buffering, you're not sending the data to the browser when you echo it, but you store it in a buffer. The data will be sent when you call ob_end_flush or you reach the end of the script. After ob_end_flush, you won't be able to send any new headers. You start output buffering with ob_start.
It is possible to use header() with require() when I use output buffering. That means that the whole script is buffered and first send when the script has come to an end.
I have done it by doing this
ob_start("ob_gzhandler"); //begin buffering the output
require_once('/classes/mysql.php');
// Some code where I access the database.
header('/somepage.php');
exit;
ob_flush(); //output the data in the buffer
I appeared for php test, their I was asked one question for which I could not find the answer.
The question is like this.
echo "MESSI is injured!!";
header("Location:somepage.php");
Interviewer want both header and echo to be written on the same page.
I wonder how's it possible.It should give some error like
headers already sent by (output started at .....
Is it really possible to write echo and header onto same page !!!
You can use Output Buffering as
ob_start();
echo "MESSI is injured!!";
header("Location:somepage.php");
ob_end_flush();
The problem is that we cannot send the header after we start sending the output. To solve this we buffer the output. The function ob_start turns output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. So the echo output will be buffered. Next we send the header without any problem as we've not yet spit out any output. Finally we call ob_end_flush to flush the internal buffer contents and to stop output buffering.
You can do it as long as all the header calls come before any non-header output is sent (this includes pesky things like newlines/whitespace). So
<?php
header("Location:somepage.php");
echo "MESSI is injured!!";
?>
should do the trick