I understand that so long as data has been sent to the browser then the headers cannot be modified.
Is there any way (using PHP) that I can perform a redirect to take a user to another page (obviously not using headers)
If so could you please point me to some documentation?
asked Aug 15, 2011 at 15:00
Decided to write my own php function which implements a javascript redirect. See the code below.
function redirect($url) { $string = '<script type="text/javascript">'; $string .= 'window.location = "' . $url . '"'; $string .= '</script>'; echo $string; }answered Aug 15, 2011 at 15:26
GaryDevenayGaryDevenay
2,3752 gold badges18 silver badges41 bronze badges
1
I assume output buffering is not an option, since you mention that data has been sent. Use meta redirects, or output JavaScript that takes care of this.
The former could be implemented by adding the following to the <head> section
<meta http-equiv="refresh" content="0; url=//example.com/">For the latter, writing something like the following could help:
<script type="text/javascript"> window.location = "//example.com/"; </script>Replace //example.com/ with your target URL. Note that the latter might be more wieldly to implement if some data has indeed been sent.
A caveat: Both these methods can be blocked at the client end, but technically, so can 301 and 302 redirects.
answered Aug 15, 2011 at 15:10
susmitssusmits
2,1702 gold badges22 silver badges27 bronze badges
2
You can use Javascript or a Meta tag. But if you do not want to fix your code to display output after processing then just turn on output_buffering in the .htaccess file for the site.
php_flag output_buffering on
Is what you would put in your .htaccess file to turn that on. It will allow redirects even if you output stuff while processing. This is because the output buffer saves the buffer to the end, so no data is sent prior to.
Remco K.
6484 silver badges18 bronze badges
answered Aug 15, 2011 at 15:10
JimJim
18.4k5 gold badges48 silver badges65 bronze badges
1
Maybe you can use the output buffer before you make a decision to redirect the page.
IMHO otherwise there is only the way to use HTML meta redirect or JavaScript redirect.
answered Aug 15, 2011 at 15:13
Eddy FreddyEddy Freddy
1,8261 gold badge13 silver badges18 bronze badges
This is the php function i use to redirect. If headers haven´t been sent, uses "header(location)", else, uses javascript function.
function redirect($url) { $baseUri=_URL_; if(headers_sent()) { $string = '<script type="text/javascript">'; $string .= 'window.location = "' . $baseUri.$url . '"'; $string .= '</script>'; echo $string; } else { if (isset($_SERVER['HTTP_REFERER']) AND ($url == $_SERVER['HTTP_REFERER'])) header('Location: '.$_SERVER['HTTP_REFERER']); else header('Location: '.$baseUri.$url); } exit; }answered Jan 21, 2014 at 11:34
PrescolPrescol
5864 silver badges12 bronze badges
PHP doe not offer any other way as far as I remember (limitation of the http protocol), but you can do this with javascript, but it is a completely different process : with js, it is the content of the page that tells the browser to perform a redirection, not the server.
answered Aug 15, 2011 at 15:10
In my case worked ob_start
Javascript solution looks so.. strange
answered Feb 7, 2014 at 11:39