Interview Question: How to Have an Echo Before Header

Interview Question: Can we have an echo before header?

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.

How to execute an echo with Java script content before header in php

try this :

echo "<script type='text/javascript'>
alert('Wrong username or password Please try again');
location = 'http://localhost/xampp/my_hospital/login.php';
</script>";

PHP output happening before Header output. Can I trace where echo is being called in PHP?

Thanks to everyone who offerred help in the comments. As it turns out, DarkBee may have been the closest to what was happening. I'm posting this because it can be useful information for someone else who has the same problem.

What was happening is that I was including another .php file at the top of the file that was having the problems. There was nothing special about that file, it just contained a few globel defines.

The problems began, when I edited the included file with cPanel's editor. Somehow it saved the file in a way that generated the strange output.

When I made modifications to that file locally and then uploaded it with FTP, the problems went away.

Just to complete the test, I edited the file online in cPanel again, and the problem returned!

So, beware of cPanel file editing. Note, I just used cPanel's 'edit' feature, not it's code editor.

Why does this header after an echo not generate an error?

I suppose you have output buffering enabled? Check your php.ini for output_buffering. If this is switched on, all printed text will first be cached in the output buffer before being sent to STDOUT.

Can send header even though I have output

Check your phpinfo(), you probably have output_buffering switched on.

Modal not showing up after login in php

I found something that might interest you here:

Interview Question: Can we have an echo before header?

The problem is that we cannot send the header after we start sending the output, and if you send the header before the echo, the echo will not be executed.

Try this:

Solution 1: (from the link above).

ob_start();
echo "<script>$('#loginsuccess').modal('show')</script>";
header("location: homepage.php");
ob_end_flush();

Solution 2: Use a javascript redirection instead of header function.

echo "<script>
$('#loginsuccess').modal('show');
window.location.replace('http://fullpath-homepage.php');
</script>";

Additional note:

You can use also:

window.location.href="http://example.com";
window.location.assign("http://example.com");

The replace method navigates to the URL without adding a new record to the history.

Can output be written without sending headers (For debugging)

You could use the ob_start function, which starts output buffering and which will result in the header function not throwing the error message.

You could also use a file (file_put_contents).

Or like Collin Grady said: If it's just for debugging, ignore the warnings, read the information you need and remove the debug prints again.



Related Topics



Leave a reply



Submit