I have a function which needs to redirect the page if a variable is set etc...
The problem is, this function is at the bottom of the php page.
This means, I have outputted alot of information, so I get a headers warning.
"Warning - Headers already sent by ..."
Is there any way to redirect after headers are sent?
Thanks
What you should do is put ob_start() at the very beginning of your page, and ob_flush() at the very end. This way you don't run into headers already sent errors.
See those functions for further reference.
There are ways, but they're basically workarounds:
The simplest one is the meta http-equiv:
<meta http-equiv="Refresh" content="0;http://www.example.com/newlocation">
some browsers will not like this when it's outside of the <head> element, and drop into quirks mode
Or, you can try the JavaScript redirect:
<script>
window.location = 'http://www.example.com/newlocation';
</script>
which obviously won't work without JavaScript.
Related
using JS : (in <head> tag)
<script>window.location="https://stackoverflow.com";</script>
using PHP : (in <head> tag)
header('Location: https://stackoverflow.com');
end();
Which one I should use ? or another ?
and what about using <meta>?
<meta http-equiv="refresh" content="0;url=https://stackoverflow.com"/>
Many good answers , I don't know which answer I will accept, Thanks so much
The result is same for all options. Redirect.
<meta> in HTML:
Show content of your site, and next redirect user after a few (or 0) seconds.
Don't need JavaScript enabled.
Don't need PHP.
window.location in JS:
Javascript enabled needed.
Don't need PHP.
Show content of your site, and next redirect user after a few (or 0) seconds.
Redirect can be dependent on any conditions if (1 === 1) { window.location.href = 'http://example.com'; }.
header('Location:') in PHP:
Don't need JavaScript enabled.
PHP needed.
Redirect will be executed first, user never see what is after. header() must be the first command in php script, before output any other. If you try output some before header, will receive an Warning: Cannot modify header information - headers already sent
A better way to set the location in JS is via:
window.location.href = 'https://stackoverflow.com';
Whether to use PHP or JS to manage the redirection depends on what your code is doing and how. But if you're in a position to use PHP; that is, if you're going to be using PHP to send some JS code back to the browser that simply tells the browser to go somewhere else, then logic suggests that you should cut out the middle man and tell the browser directly via PHP.
It depends on how and when you want to redirect the user to another page.
If you want to instantly redirect a user to another page without him seeing anything of a site in between, you should use the PHP header redirect method.
If you have a Javascript and some action of the user has to result in him entering another page, that is when you should use window.location.
The meta tag refresh is often used on download sites whenever you see these "Your download should start automatically" messages. You can let the user load a page, wait for a certain amount of time, then redirect him (e.g. to a to-be-downloaded file) without Javascript.
PHP redirects are better if you can as with the JavaScript one you're causing the client to load the page before the redirect, whereas with the PHP one it sends the proper header.
However the PHP shouldn't go in the <head>, it should go before any output is sent to the client, as to do otherwise will cause errors.
Using <meta> tags have the same issue as Javascript in causing the initial page to load before doing the redirect. Server-side redirects are almost always better, if you can use them.
The first case will fail when JS is off. It's also a little bit slower since JS must be parsed first (DOM must be loaded). However JS is safer since the destination doesn't know the referer and your redirect might be tracked (referers aren't reliable in general yet this is something).
You can also use meta refresh tag. It also requires DOM to be loaded.
window.location.href = 'url';
is beter than
header('location:url');
because the header command is mustly return an error "Warning: Cannot modify header information - headers already sent"
using js window.location.href = 'url';
this is beter
I am setting a parameters in my PHP page and need to pass it to another WEB SITE, to execute operation on the WEB SITE (i don't need to get re result). I use header ("location : https://localhost/test=123") but its not executing it and display:
Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/script1.php:33) in /Applications/XAMPP/xamppfiles/htdocs/script1.php on line 106
How can i simply just invoke it one time?
if there is solution from bash file,also can help...
The header function can only be used before ANY output is displayed on the screen.
For example:
<?php
echo( 'test' );
header('location: www.google.com');
?>
Would fail with that error you reported.
You already output content to use header().
I would recommend:
$sRemotePage = file_get_contents('https://localhost/test=123');
More here: http://php.net/manual/en/function.file-get-contents.php
You can use the HTTP meta element to set where the browser redirects the user. It needs to look like this:
<meta HTTP-EQUIV="Refresh" content="0; url=http://localhost/test=123">
Where 0 is the number of seconds the browser will wait before redirecting, and url is the redirected URL.
The other option would be using Output Buffering, but I am not entirely sure what you want to do and whether it can include ob_ or not.
Bot of these lines:
echo '<br/><br/>'.$_SERVER["SCRIPT_NAME"]."?page=".$pager->GetVariableC."&threadID=".$threadID;
header("Location:".$_SERVER["SCRIPT_NAME"]."?page=".$pager->GetVariableC."&threadID=".$threadID);
Give me this:
/PoliticalForum/Thread/thread.php?page=2&threadID=6
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\PoliticalForum\Thread\comments.php:42) in C:\xampp\htdocs\PoliticalForum\Thread\thread.php on line 348
How do I redirect at the end of the script?
When this error happens, you have already sent something to the browser, by using echo or by having a couple of new lines after the closing php tag. Be sure not to have any new lines or echoing something before redirecting.
You can't redirect after you've sent output to the client's browser using PHP's header().
What you can do is use a meta tag:
<meta http-equiv="refresh" content="2;url=http://www.destination.com/">
Where 2 is the time before the redirect in seconds, and the url is the destination. You can find more information about it here (you should read the drawbacks section).
You can't redirect after you've already sent any data to the browser, in this case the first line is doing so. Why are you trying to print something that you are never going to see (The browser would be immediately redirected)?
Either remove the outputting lines above the redirect if possible, or look into using output buffers if you can't modify the surrounding code.
If you want output and a redirection then you need to use a javascript redirection. http://www.tizag.com/javascriptT/javascriptredirect.php
What you're doing here is called an HTTP redirection (an HTTP 302), which is one of the HTTP headers sent to the browser as a response.
This error message is due to the fact that you echo'd content which has the effect of sending out all the buffered HTTP headers then tried to set a header (but it was already sent). To get a much better view of this I recommend all web developers install firebug and monitor the "Network" tab, you'll really get a better grasp on headers and their meanings.
Cheers
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.
there is one think, i can't understand anyway:(((
when i try to set cookie(it is on line 28 in login.php), browser returns me an error!!!
Cannot modify header information - headers already sent by (output started at C:\xampp2\htdocs\video\index.php:9) in C:\xampp2\htdocs\video\login.php on line 28
but on line 9 in index php, i haven't any header!!! there is a tag!!!
i cant understand it!!! can somebody tall me why it returns me such error?
Cookies are sent as headers. From the PHP docs for setcookie:
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.
Also, if your page is saved in UTF-8 format, the BOM (Byte Order Mark) will stop you from being able to set any headers, as it counts as output. See http://bugs.php.net/bug.php?id=22108. To get around this you need to save your file without the byte order mark.
See also: Byte order mark#Unwanted BOMs - Wikipedia
You can't print out anything on the site before sending a Header.
set the cookie first before any html tags (a.k.a output), even before declaring !DOCTYPE html or head, header information and the like. for example your file could look something like:
<?php setcookie("oreo", $value, time()+(60*60*24*30));?>
<?php setcookie("vanilla_wafer", $wafer, time()+(60*60*24*30));?>
<!DOCTYPE html>
<head>
<title>Cookies and Milk</title>
</head>
<body>
<p>yo</p>
</body>
</html>
Please post some code.
What this error means is, that something was already sent (can also be an echo, error notice etc.).
You have to have your header functions at the very top of your application. Like basically the first lines are for header();