Interview Question: Can we have an echo before header? - php

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

Related

ob_start() and ob_implicit_flush(true) using at the same time cause an issue

I need to echo some output while executing php file ,beacuse execution takes 10 sec and end of 10sec page should be directed via header("Location:test.php)
However If I use ob_start and ob_implicit_flush(true) at the same time , we cannot direct page and getting
Warning: Cannot modify header information - headers already sent by
I also need to use ob_implicit_flush(true) to print output while execution.
How can I display output and direct page ?
You cannot output both body content and a redirect header in the same response, much less output the body before the header. The HTTP headers come first, so you cannot output a body before headers and you cannot output headers after the body has been output. Further, a redirect header causes the request to be immediately redirected before the browser will display any of its content, so the entire thing doesn't work on two levels.
If you want to display anything while the server is doing something, you'll need to use Javascript in some form or another.
If you use header() function there MUST be no output BEFORE it. But of course it can be after using the function.
ob_start starts the buffer to work while ob_implicit_flush tells to use no buffer at all. So the two functions cannot be combined that way.
Example:
ob_start(); //set buffer on
print('Hello World'); //no output since buffer on
ob_implicit_flush(true); //buffer switched off again
print('Test'); //prints 'Hello World' and 'Test'
header('Location: ...'); //ERROR: output already done
You have now to decide if you want to output information from your script OR make the redirection.
Output something AFTER header is possible but it makes no sense as you will not see it anymore.
Maybe you can use the ob_get_length() function to check if there was some output and then decide if you switch page or output the buffer:
ob_start(); //set buffer on
print('Hello World'); //no output since buffer on
if(ob_get_length() > 0)
ob_end_flush();
else
header('Location: ...'); //will not be executed if output was generated

header is not cleared after executing ob_get_clean

I have a php file as the following :
<?php
ob_start();
header("Location:http://google.com");
echo "hello world";
die(ob_get_clean());
?>
I expect to see hello world on my browser,But It redirects to google.
ob_get_clean should clear the output including headers, Right?
And the result should be die("hello world");
So why is this happening?
No, headers are not buffered. From the documentation:
This function will turn 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.
header("Location:http://google.com");
It means page is redirected to google.com as buffer initiate.
Buffer is used to Store the Output data but in this case page is redirected to other site. So place of code is not correct.

Why ob_start() solves the header() error?

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.

PHP redirect without header() or meta

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

why do I need to end my ob_start()?

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.

Categories