Prevent 'headers already sent' [duplicate] - php

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
When you try to redirect an user with Header:Location to another page when there is already 'output' on the page, PHP will display the message: headers already sent.
Is it a good solution to create a function, and call that function wherever you need it (even at the bottom of the page)?
Thanks!
Example:
function redirectMe($location){
header('Location:'.$location);
}
//HTML
//Redirect user:
redirectMe('http://www.google.com');

No, that won't work because you have already output HTML by the point that the function is called.
If you want to use a redirect, it has to be before ANYTHING is output to the user. Check the variables before you output anything, the either do so and that's it, or decide not to redirect the user - then output all your HTML.
Edit: In an utterly horrible way though, if you can't work out how to check the conditions to redirect/stay, you could possibly put all your HTML into a string, then at the end of the page echo it out - but lordy, doing that would be much more work than checking the correct conditions and going from there.

A good solution would be to use ob_start() and ob_end_clean()
ob_start();
// all your PHP/HTML codes go there.
$output = ob_get_contents();
ob_end_clean();
more http://php.net/manual/en/function.ob-start.php‎

If anything is getting outputted to the browser before the execution of the header function for redirecting, it will give headers already sent error. If no output is needed to be shown to user, you can use ob_start() function at the start of your page to buffer the output beofre being sent to the browser and use ob_flush after the header. You can use it according to the flow of your page

Related

PHP: Header wont redirect [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
When i put my website online, all of the headers will not work. But when i built it, just on a local database etc. it worked "Works" fine.
I get this ERROR on the website currently:
Warning: Cannot modify header information - headers already sent by (output started at /customers/5/8/4/infuze.dk/httpd.www/index.php:22) in /customers/5/8/4/infuze.dk/httpd.www/includes/functions.php on line 16
I used my google foo a little and as far as i can understand is that i cant output any data before i use headers. But i dont thing that is the case here.
Here is the code i used in on the site.
if(empty($_GET['page'])){
header('location: index.php?page=homepage');
}
or at least one example
I usually get the error "Cannot modify header information - headers already sent by" when the page you're trying to redirect from contains information. Check to see if the script uses echo, include, require, etc., or contains HTML markup before the line at which you try to redirect.
<p>Hello!</p>
<?php
header('location: index.php?page=homepage');
?>
Will NOT work, because it sends the browser information before trying to redirect.
<?php
echo "Hi!";
header('location: index.php?page=homepage');
?>
Also won't work, because it also sends the browser information before trying to redirect.
<?php
header('location: index.php?page=homepage');
include('setup.txt');
?>
<h1>Amazing page!</h1>
WILL work, because it tries to send the browser information AFTER the redirect, but not before.
Headers won't redirect in the following conditions:
Any blank space is remaining in the file after ?> tag.
To avoid this, please avoid ending ?> tag (file end).
Please check whether, any echo or print statement is written accidentally.
Also, please check whether any HTML tag is remaining there.
If you follow, all this, your script will work.
Finally, in the starting of the file, put
ob_start()
This function will store all your page's output in a buffer and thus your redirection will work.
Try this;
<?php
ob_start();
// code
ob_end_flush();
?>
>> Call ob_start() at start of the script.
>> call ob_end_flush() at the end of script.
Thanks!

how to jump to other page after header had been sent? [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
how to jump to other page after header had been sent ?
I have some codes writed in my template file, I want to using php to jump to other page when some condition is true, but I can not use header command here ,for the header info had been sent in other php file before this template file. then how can I jump to other url now ? only the way to use js?
Warning: Cannot modify header information - headers already sent by ...
Use output buffering and header clearing to achieve this.
ob_start()
...
if (!headers_sent()) {
foreach (headers_list() as $header)
header_remove($header);
}
ob_flush_end()
Untested, but give it a shot.
The anaswer from CP510 is good. Another way would be to use javascript to redirect the page. To quote Ryan McGeary answer, you could do either
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
or
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
you have two options.
Move your header location before sending any output.
use a client side redirection and you have two ways to do this
a/ using javascript
window.location = "http://www.yoururl.com";
b/ using meta
<META http-equiv="refresh" content="5;URL=http://www.yourlink.com/new-link">
The best solution here How to fix "Headers already sent" error in PHP

Header location issue, how to solve it? [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I created a web site, with php and SQL using WampServer.
And after a condition or in boucle I use Header location to transfer the user to another page. But I got an error from the server, I think is because I use the header after a code and not in . I deleted all blank spaces.
if($passfinal['contrasena']==$_POST['password'])
{
$_SESSION['logedin']=TRUE;
$_SESSION['userid']=$passfinal['id'];
header('Location: ../index.php');
}
Do you have something to help me?
Thnak you.
I think, here is problem $_POST['password'], because before you make compare, first you must check if(isset($_POST['password'])), blank spaces isn't important in this case and last one: I advise you, that you must write full url in header function, like this: header('location: http://example.com/index.php'), because this is more nice and true way.
Warning: Cannot modify header information - headers already sent by (output started at /home/sirobdco/public_html/login/login.php:11) in /home/sirobdco/public_html/login/includes/loginform/loginform.php on line 37
You already sent headers so PHP cannot send them again!
That is, before your code
header('Location: ../index.php');
You already send headers - blank space in html, an echo in PHP, etc.
Have a read through this:
http://php.net/manual/en/function.header.php
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. The same problem exists when using a single PHP/HTML file.

Can i change header redirect in php to javascript redirect with same status in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
My site is http://www.seoitc.com, we are using joomla for this site, but have 1 problem when i try auto redirect to anypage that show error: Warning: Cannot modify header information - headers already sent by ().
I tried change redirect to use js but cant use same status(302,303...) same as in php. Please help to fix this problem please.
Thanks!
Any output is being sent before you call your header function. Check for any html code before your header call, even a white space or a blank line will cause that
According to PHP Manual
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. The same problem
exists when using a single PHP/HTML file.
You can use ob_start() at the top of the page. to prevent error.
but also you can use this code. if any error happens in your page it will redirect you to the $page:
// page address
function redirect($page)
{
header("Location: ".$page);
}
set_error_handler("redirect");
And you can check this out: Headers already sent by PHP

Php redirect to url [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 4 years ago.
I am trying to show some page that automatically redirect to another page, when using header(location) I get this error:
Warning: Cannot modify header information - headers already sent by (output started at public_html/headerFile.php)
What else should I try?
The longer, and more correct way, is to reorganize your logic so that it knows that it should redirect the user before it tries outputting anything to the page. Do all of your logic first, then display.
The quick easy way around is to add ob_start() to the beginning of your script. This turns on output buffering, so nothing gets sent to the browser until everything is finished, which means you can still modify the headers.
Try this:
echo '<meta http-equiv="Refresh" content="1;URL=http://www.google.nl">';
That should work, it's not ideal. But it will do the trick.
When using header(), you have to make sure that you have not sent any output to the user. This includes print_r, echo or any errors or warnings generated by PHP. Even whitespace counts as output.

Categories