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

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

Related

Alternative to header(location) php function using Javascript or something else [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 6 years ago.
I am writing a php-code which redirect me from localhost/login.php to localhost/index.page.
I tried using the header('Location: index.php) method, but i keep receiving an error
Warning: Cannot modify header information - headers already sent by...
The error according to my search is due to a sort of output which is sent before the header() function. Unfortunately, after trying for hours, I could not figure out what is wrong with the code, so I am now looking for an alternative method.
Just another method, to redirect me from my "localhost/login.php" page back to my home page "localhost/index.php".
Instead of using PHP to do the redirect, use a META refresh that causes your browser to follow it:
<meta http-equiv="refresh" content="0; url=http://example.com/">
Information on META redirects: Meta_refresh

Having trouble with php code to redirect [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
I am a beginner in php..
I need to redirect from one php file to another php file..
I used this code for this.
header("Location: stu_rep.html");
but it shows this error..
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\auttvl\admin\stu_rep_view.php:241) in C:\xampp\htdocs\auttvl\admin\stu_rep_view.php on line 492
I need to know what is wrong,Is there any other way to redirect without using header?
Thanks!
Header(); sends a HTTP header. But HTTP headers can only be sent before anything else. In your case, you are probably printing something out before using header();. Remove the printings and your done.
More informations here:
http://www.php.net/manual/de/function.header.php
Headers must be sent before any output is generated on the website. To "work around" this, you can add ob_start(); to the top of your file.

Cannot modify header information [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
my site works well in localhost (everything works fine)
but when I upload it in web and try to redirect the page after updating the data it says -> Cannot modify header information
95% you are trying to set a cookie, mime-type or redirection (short: http header) after you have sent http body, i.e. content. Check if you you have anything including whitespaces outside the php delimeters.
If you use the header function you have to be careful... You can't send any header before the "header location".
ob_start('mypage');
<html>
// Your code ...
</html>
ob_end_flush();
What's the error ? (php.ini, active the errors)
Else you can use the ob_start() function !

Prevent 'headers already sent' [duplicate]

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

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